如何关闭锁定的电脑(vb.net)?

发布于 2024-09-18 21:22:07 字数 179 浏览 11 评论 0原文

在 vb.net 中关闭电脑很容易:

Process.Start("shutdown", "-s -t 00")

除非用户锁定了电脑,在这种情况下上述操作将失败。

如何在 vb.net 中解决这个问题?如何关闭锁定的电脑?

该程序将在本地运行。

Shutting down a pc in vb.net is easy:

Process.Start("shutdown", "-s -t 00")

unless the user has locked the pc in which case the above fails.

How do I get around this in vb.net? How do I shutdown a locked PC?

The program will be running locally.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

内心荒芜 2024-09-25 21:22:17

使用 System.Management 命名空间比启动外部工具更优雅。以下是 C# 代码示例,应该相当容易转换:

http://www.dreamincode.net/forums/topic/33948-how-to-shut-down-your-computer-in-c%23/

Using the System.Management namespace is more elegant than starting an external tool. Here is a code example in C#, which should be fairly easy to convert:

http://www.dreamincode.net/forums/topic/33948-how-to-shut-down-your-computer-in-c%23/

寄居者 2024-09-25 21:22:15

查看 CodeProject 上的这篇文章,其中说明了强制远程关闭计算机为您提供如何操作的想法。

Have a look at this article on CodeProject which illustrates forcing a computer to shutdown remotely to give you an idea on how to do it.

贪了杯 2024-09-25 21:22:14

对于后代:

Dim ms As ManagementScope = New ManagementScope("\\LocalHost")
    ms.Options.EnablePrivileges = True

    Dim oq As ObjectQuery = New ObjectQuery("SELECT * FROM Win32_OperatingSystem")
    Dim query1 As ManagementObjectSearcher = New ManagementObjectSearcher(ms, oq)
    Dim queryCollection1 As ManagementObjectCollection = query1.Get()

    For Each mo As ManagementObject In queryCollection1
        Dim ss As String() = {"5"}
        mo.InvokeMethod("Win32Shutdown", ss)
    Next

Google“Win32Shutdown”以获取可用标志的更多详细信息(上面的 ss)。 5 是在电脑锁定时强制关机,但它比 shutdown /f 更优雅,并且不会在重新启动时导致程序或服务出现任何问题。

For posterity:

Dim ms As ManagementScope = New ManagementScope("\\LocalHost")
    ms.Options.EnablePrivileges = True

    Dim oq As ObjectQuery = New ObjectQuery("SELECT * FROM Win32_OperatingSystem")
    Dim query1 As ManagementObjectSearcher = New ManagementObjectSearcher(ms, oq)
    Dim queryCollection1 As ManagementObjectCollection = query1.Get()

    For Each mo As ManagementObject In queryCollection1
        Dim ss As String() = {"5"}
        mo.InvokeMethod("Win32Shutdown", ss)
    Next

Google "Win32Shutdown" for more details of the flags available (ss above). 5 is a forced shutdown for when the pc is locked but it's more graceful than shutdown /f and doesn't appear to cause any problems with programs or services on restart.

撩发小公举 2024-09-25 21:22:13

我认为您正在寻找“-f”标志来强制关闭。

引用 MS 知识库文章
当计算机被锁定时,如果您与 -f 选项一起运行 Shutdown.exe 命令,则可以关闭计算机。

I think you're looking for the '-f' flag to force a shutdown.

Quote from a MS KB article:
When the computer is locked, you can shut down the computer, if you run the Shutdown.exe command together with the -f option.

素罗衫 2024-09-25 21:22:12

你可以 P/Invoke ExitWindowsEx

有一个 C# 示例在那里,但我相信你可以转换它。

You could P/Invoke ExitWindowsEx

There is an example in C# there, but I'm sure you can convert it.

半步萧音过轻尘 2024-09-25 21:22:11
System.Diagnostics.Process.Start("shutdown", "-s -f -t 00")

这将在 00 毫秒内强制关闭。您必须调用每个进程的代码是多余的,请使用上面的代码。只需在顶部执行 System.Imports.IO 即可。

System.Diagnostics.Process.Start("shutdown", "-s -f -t 00")

This will force a shutdown In 00ms silently. The code you have to invoke each process is redundant, use the code above. Just do a System.Imports.IO at the top and you are good to go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文