C # remotely restart the computer

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. Management;

Restart Remote Computer in namespace
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}

Private void btn_reboot_click (Object sender, eventargs E)
{
// Define some options for connecting to the remote computer
Connectionoptions Options = new connectionoptions ();
Options. Username = textbox2.text;
Options. Password = textbox3.text;
Managementscope scope = new managementscope ("\\\\" + textbox1.text + "\\ Root \ cimv2", options );
Try
{
// Connect the remote computer with the given administrator username and password
Scope. Connect ();
Objectquery OQ = new objectquery ("select * From win32_operatingsystem ");
Managementobjectsearcher query1 = new managementobjectsearcher (scope, OQ );
Managementobjectcollection querycollection1 = query1.get ();
Foreach (managementobject Mo in querycollection1)
{
String [] Ss = {""};
If (combobox1.text = "restart ")
{
Mo. invokemethod ("reboot", SS );
}
If (combobox1.text = "Disable computer ")
{
Mo. invokemethod ("shutdown", SS );
}
// If (combobox1.text = "logout ")
//{
// Mo. invokemethod ("logoff", SS );
//}
}
}
Catch (exception ER)
{
MessageBox. Show ("connection" + textbox1.text + "error, error message:" + Er. Message );
}
}

Private void form1_load (Object sender, eventargs E)
{
Combobox1.items. Add ("restart ");
Combobox1.items. Add ("Disable computer ");
// Combobox1.items. Add ("deregister ");
}

}
}

Related Article

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.