简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM