user3929914
user3929914

Reputation: 151

How to restart a windows device from my windows 8.1 store app using c# code in visual studios?

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?

Upvotes: 1

Views: 234

Answers (1)

Michael Kreich
Michael Kreich

Reputation: 175

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

Upvotes: 1

Related Questions