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?
大家好我正在使用visual studio,c#和XAML创建一个Windows 8.1应用程序。我想知道是否有一些我可以使用的c#代码可以在应用程序运行时自动重启用户的Windows设备?所以,为了清楚,用户会按下我的应用程序中的按钮,例如他们的设备可能会提示他们然后重启?
1 个解决方案
#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
编辑(关于上面关于WinRt的评论):如果使用WinRT,则可能会对以下帖子感兴趣:从metro app关闭windows 8
#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
编辑(关于上面关于WinRt的评论):如果使用WinRT,则可能会对以下帖子感兴趣:从metro app关闭windows 8