明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺!

C#遠(yuǎn)程關(guān)機(jī) C#遠(yuǎn)程關(guān)機(jī)代碼

[摘要]C#遠(yuǎn)程關(guān)機(jī) C#遠(yuǎn)程關(guān)機(jī)代碼 WMI中Win32_OperationSystem的方法Win32ShutDown(flag)中flag的參數(shù)可以是下表中的任意一種: 值 描述 0 注銷 0 + 4...

C#遠(yuǎn)程關(guān)機(jī) C#遠(yuǎn)程關(guān)機(jī)代碼

 

WMIWin32_OperationSystem的方法Win32ShutDown(flag)flag的參數(shù)可以是下表中的任意一種: 

 描述 
0 
注銷 
0 + 4 
強(qiáng)制注銷 
1 
關(guān)機(jī) 
1 + 4 
強(qiáng)制關(guān)機(jī) 
2 
重起 
2 + 4 
強(qiáng)制重起 
8 
關(guān)閉電源 
8 + 4 
強(qiáng)制關(guān)閉電源

下面是示例:

//
關(guān)閉計(jì)算機(jī)
private void btn_Shutdown_Click(object sender, EventArgs e)
{
    string IPShutdown = "192.168.1.100";

    DialogResult dlResult = MessageBox.Show("確實(shí)要關(guān)閉“" + IPShutdown + "”電源嗎?", "請確認(rèn)", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
    if (dlResult == DialogResult.Yes)
    {
        string[] inParams ={ "8", "4" };
        BootComputer ShutdownBootComputer = new BootComputer();
        ShutdownBootComputer.strIp = IPShutdown;
        ShutdownBootComputer.strAdmin = txtAdmin.Text.Trim();
        ShutdownBootComputer.strPassword = txtPassword.Text.Trim();
        ShutdownBootComputer.strMothod = "Win32Shutdown";
        ShutdownBootComputer.inParams = inParams;
        ShutdownBootComputer.BootMachine();
    }
}

//關(guān)閉重啟計(jì)算機(jī)(支持多線程)
public class BootComputer
{
    public string strIp, strAdmin, strPassword, strMothod;
    public string[] inParams;
    public void BootMachine()
    {
        ConnectionOptions BootConn = new ConnectionOptions();
        BootConn.Username = strAdmin;
        BootConn.Password = strPassword;
        ManagementScope ms = new ManagementScope("\\\\" + strIp + "\\root\\cimv2", BootConn);
        ms.Options.EnablePrivileges = true;
        if (!string.IsNullOrEmpty(strAdmin) && !string.IsNullOrEmpty(strPassword))
        {
            try { ms.Connect(); }
            catch { }
        }
        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 = inParams;
                    mo.InvokeMethod(strMothod, ss);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(strIp + ":" + ex.Message + "
網(wǎng)絡(luò)不通或用戶名、密碼不正確!");
            }
        }
    }
}

 


學(xué)習(xí)教程快速掌握從入門到精通的電腦知識