1

Hi guys I am creating a windows 8.1 application using visual studios, c# and XAML. I was wondering if there is some c# code I could use that could restart the users windows device automatically whilst the app was running? So just to be clear the user would press a button in my application for example and their device would maybe prompt them then restart?

user3929914
  • 151
  • 1
  • 2
  • 11
  • Is this a WinRT app or a WPF app? You tagged both, but your question seems to be for WinRT. (If so, the answer below by josef_wurzel is not good, because WinRT won't let you do what he wrote (as that's a WPF solution). – Tamás Deme May 06 '15 at 10:50

1 Answers1

1

Some code that I was using:

    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool InitiateShutdown(string lpMachineName,
        string lpMessage,
        UInt32 dwGracePeriod,
        UInt32 dwShutdownFlags,
        UInt32 dwReason);

    public static void ShutdownWorkstation()
    {
        UInt32 flags = 0x8;
        UInt32 reason = 0;
        UInt32 gracePeriod = 5;
        InitiateShutdown(System.Environment.MachineName, "", gracePeriod, flags, reason);
        //Console.WriteLine("Dummy Shutdown");
    }

    public static void RestartWorkstation()
    {
        UInt32 flags = 0x4;
        UInt32 reason = 0;
        UInt32 gracePeriod = 5;
        InitiateShutdown(System.Environment.MachineName, "", gracePeriod, flags, reason);
        //Console.WriteLine("Dummy Restart");
    }

edit (regarding the comment about WinRt above): If using WinRT then the following post might be of interest: shutdown windows 8 from metro app

Community
  • 1
  • 1