Is it possible to restart the computer using pure .NET and * without * using p / invoke?

Is it possible, without using pinvoke, to restart the PC using .NET?

I kind of just repeated the name, but I'm not too sure how to do it next!

Edit:

I should have mentioned that I did not want to use "shutdown -r" as a solution.

I was in a really clean .NET way, something like:

Environment.ShutDown();

In other words, something that is supported with .NET as new versions of Windows arises.

Edit 2:

Please stop asking "what's with p / invoke." These kinds of answers are exactly what SO users love; the alleged "side" approach to answering the question. However, although there is no real problem with p / invoke, and I use it with pleasure, which is actually not the case if you ask if .NET has a more formal way to achieve something? If it is in .NET, then any API changes between the OS will (most likely) be reflected. Whatever the reason, is this not a crime aimed at minimizing the use of DLL imports?

I'm sure I included something in the question like:

 [DllImport("something32.dll")] static extern int ClimbWall32Ex(IntPtr32 blah); 

And you could just do:

 SomeNamespace.ClimbWall(); 

Everyone here shouts: "What's wrong with using SomeNamespace.ClimbWall(); ;?"

Sigh.

+6
c # pinvoke restart dllimport
source share
3 answers

You need to call ExitWindowsEx, accessible only through DllImport

+7
source share

Not sure why you are not just using P / Invoke, but one alternative way to restart should be to use System.Diagnostics.Process.Start in connection with the shutdown .

Example:

 System.Diagnostics.Process.Start("shutdown", "-r"); 


If this is also unacceptable, you can study WMI (see here for an example, which can probably be modified to suit your goals).

+15
source share

You can use WMI to reboot. Below from the memory, but I think it is pretty close, although a little messy. :)

 var computer = "COMPUTERNAME"; var query = string.Format("SELECT * FROM Win32_OperatingSystem"); ManagementScope scope; var computerPath = string.Format(@"\\{0}\root\cimv2", computer); scope = new ManagementScope(computerPath); scope.Connect(); var q = new ObjectQuery(query); var s = new ManagementObjectSearcher(scope, q); ManagementObjectCollection qr; qr = s.Get(); foreach (ManagementObject r in qr) { string[] p = { "" }; r.InvokeMethod("Reboot", p); } 
+10
source share

All Articles