gpt4 book ai didi

vb.net - 重新启动 PC,停止使用 VB.NET 进行编码

转载 作者:行者123 更新时间:2023-12-05 09:24:36 25 4
gpt4 key购买 nike

如何使用 VB.Net 框架 2.0 或更高版本重新启动 PC

我也喜欢为重启过程设置定时器。

VB.net框架是否支持以上过程?我需要添加库还是 DLL 文件

提前致谢。

最佳答案

引用:Programmatically restart computer from .NET

您可以使用不同的方法通过 C# 代码关闭/重新启动计算机。以下是其中一些:

  1. 使用 Win32 API。

  2. 使用“关机”命令。即内部触发 shutdown.exe/s

使用“关机”命令:

代码片段

System.Diagnostics.Process.Start("ShutDown", "/r")
// If immediately then use
// System.Diagnostics.Process.Start("ShutDown", "/r /t 00")



/s = shutdown
/r = restart
/t = timed shutdown

在命令提示符处执行 "shutdown/?" 以了解有关该命令的更多信息。

引用:Rebooting a Windows Box Programmatically
另一种方法是Win32 API的ExitWindowsEx API函数

我们需要做的第一件事是引用 InteropServices 命名空间,以便我们可以访问 Windows API 函数。为此,请在表单代码的开头添加一个新的 using 指令。

using System.Runtime.InteropServices;

将下面的声明放在窗体的类代码块中

[DllImport("user32.dll", SetLastError = true)]
static extern int ExitWindowsEx(uint uFlags, uint dwReason);

然后调用这个方法..

    BOOL b = ExitWindowsEx( EWX_REBOOT|EWX_FORCE,
SHTDN_REASON_MINOR_MAINTENANCE |
SHTDN_REASON_FLAG_PLANNED);

更多引用:
Shutdown, Restart, or Log Off your computer using VB.Net
Simplest way to Programmatically ShutDown or Restart your System using C# or VB.NET

关于vb.net - 重新启动 PC,停止使用 VB.NET 进行编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10527659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com