C # Shutdown

Source: Internet
Author: User

Introduction to the Shutdown command:
Usage: shutdown [/I |/l |/s |/r |/a |/p |/h |/e] [/f]
[/M // computer] [/t xxx] [/d [p:] xx: yy [/c "comment"]
No parameter display help. This corresponds to the type /? Is the same
/? Show Help. This is the same as not typing any options
/I displays the graphical user interface (GUI ).
This must be the first option
/L log out. This cannot be used with the/m or/d option
/S shut down the computer
/R Disable and restart the computer
/A discard the system shutdown.
This can only be used during timeout
/P shut down the local computer without timeout or warning.
This can only be used with the/d option
/H sleep the local computer.
This can only be used with the/f Option
/E. Record the cause of computer shutdown in the document
/M // specify the target computer
/T xxx sets the timeout before closing to xxx seconds.
Valid range: 0-600; default value: 30
/C "comment" comments about the reason for restarting or disabling.
Up to 127 characters allowed
/F force the running application to close without warning the user beforehand
/D [p:] xx: yy provides reasons for restarting or disabling
P indicates that the restart or shutdown is planned.
Xx is the primary cause Number (a positive integer smaller than 256)
Yy is the secondary cause Number (a positive integer smaller than 65536)
The Code is as follows:
System. Diagnostics. Process bootProcess = new System. Diagnostics. Process ();
BootProcess. StartInfo. FileName = "shutdown ";
BootProcess. StartInfo. Arguments = "/s ";
BootProcess. Start ();

Later, I thought about it carefully and provided a complete class library for System shutdown, restart, and logout in the application. The DLL file is System. ManagerMent. dll, which is used by WMI.

WMI is short for Windows Management Instrumentation. Its function is mainly to access some information and services on the local host and manage remote computers (of course, you must have sufficient permissions ), for example, restart, shut down, shut down the process, and create a process. WMI contains the language WOL for various operations. The following code implements shutdown:

ConnectionOptions BootConn = new ConnectionOptions ();
BootConn. Username = "XXXX ";
BootConn. Password = "XXXXXX ";
ManagementScope MS = new ManagementScope ("// computer name or IP address // root // cimv2", BootConn );
Ms. Options. EnablePrivileges = true;
Try
{
Ms. Connect ();
}
Catch (Exception err)
{
MessageBox. Show (err. ToString ());
}
If (ms. IsConnected)
{
Try
{
ObjectQuery oq = new ObjectQuery ("SELECT * FROM Win32_OperatingSystem ");
ManagementObjectSearcher mos = new ManagementObjectSearcher (MS, oq );
ManagementObjectCollection moc = mos. Get ();
Foreach (ManagementObject mo in moc)
{
String [] ss = {"8", "4 "};
Mo. InvokeMethod ("Win32Shutdown", ss );
}
}
Catch (Exception ex)
{
MessageBox. Show (ex. Message );
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.