Reputation: 270
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.
Upvotes: 3
Views: 3431
Reputation: 21
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.
Upvotes: 2
Reputation: 270
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.
Upvotes: 1
Reputation: 172270
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/
Upvotes: 0
Reputation: 55009
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.
Upvotes: 1
Reputation: 65496
You could P/Invoke ExitWindowsEx
There is an example in C# there, but I'm sure you can convert it.
Upvotes: 1
Reputation: 34592
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.
Upvotes: 0