Rebooting

  • Thread starter Thread starter A
  • Start date Start date
As far as I know, there is no managed .NET support for this.
However, you can use the "ExitWindowsEx" or "InitiateShutdown" or
"InitiateShutdownEx" Windows API calls.
The first one should be used if initiated from an interactive application,
the last two should be used from
a server application,

Bennie Haelen
 
Hi,

Thank you for posting in the community!

Based on my understanding, you want to use C# to reboot your computer.
===========================================
Based on my experience, .Net class library does not support the function of
rebooting. You need to get this done through P/invoke some WIN32 APIs.

Bennie has provided you the functions you may use, please look the detailed
remarks in MSDN "PlatForm SDK"

===========================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
You can use the System.Management interfaces to reboot a machine. This uses
WMI (I think it stands for Windows Management Instrumentation ). This will
also allow you to reboot remote machines too if you wanted to.

Here's some code, may need some changes, I just copy/pasted from my code. If
the current user has permissions to reboot the machine, you can comment out
the connection_options stuff also.

using System.Management;

/// ----------------- Need class dedfinition...

int result;

result = 0;

string host;

host = "LOCALHOST";

ConnectionOptions Connect_options = new ConnectionOptions();

Connect_options.Username = user; // enter username

Connect_options.Password = password; // enter password

ManagementScope scope = new
ManagementScope("\\\\"+host+"\\root\\cimv2",Connect_options);

scope.Options.EnablePrivileges = true;

scope.Connect();

System.Management.ObjectQuery oq = new System.Management.ObjectQuery("SELECT
* FROM Win32_OperatingSystem");

ManagementObjectSearcher query1 = new ManagementObjectSearcher(scope,oq);


ManagementObjectCollection queryCollection1 = query1.Get();

foreach( ManagementObject mo in queryCollection1 )

{

string[] ss={"6","0"};

mo.InvokeMethod("Win32Shutdown",ss);

Console.WriteLine(mo.ToString());

}
 
Hi

Is your problem resolved?
If you still have anything unclear, please feel free to feedback to me. I
will help you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top