遠程關閉計算機


實現代碼:

  

知識運用:

  ConnectionOptions類  //指示生成WMI連接所需要的所有設置

    其Username  Passworr屬性

  ManagementScope類  //表示管理操作的范圍  (命名空間)

    public ManagementScope (string path , ConnectionOption  options)

    public void Connect()  //該方法用來將此ManagementScope連接到實際的WMI范圍

實現代碼:

        /// <summary>
        /// 關閉或重啟遠程計算機
        /// </summary>
        /// <param name="doinfo">執行的操作</param>
        public void CloseComputer(string doinfo)
        {
            ConnectionOptions co = new ConnectionOptions();                //創建ConnectionOptions對象
            co.Username =textBox2.Text;                                    //設置遠程計算機用戶名  
            co.Password = textBox3.Text;                                   //設置遠程計算機登陸密碼
            ManagementScope ms = new ManagementScope("\\\\" + textBox1.Text + "\\root\\cimv2:Win32_Service", co);
            try
            {
                ms.Connect();                                               //連接遠程對象
                ObjectQuery obj1 = new ObjectQuery("select * from Win32_OperationSystem");  //創建ObjectQuery對象
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(obj1);     //創建ManagementObjectSearcher對象
                ManagementObjectCollection collection = searcher.Get();                     //得到WMI控制
                foreach(ManagementObject m in collection)
                {
                    string[] str = {""};
                    m.InvokeMethod(doinfo,str);
                }
            }
            catch (Exception et)
            {
                MessageBox.Show(et.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CloseComputer("Shutdown");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            CloseComputer("Reboot");
        }

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM