今天通过远程桌面重启服务器失败了,远程桌面就登录不进去了,不过还是能ping通的,为了能接着工作,就写了下面的代码去重启服务器。使用System.Mangement命名空间需要引用System.Mangement。
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;

namespace Test
{
    public partial class RebootServer : Form
    {
        public RebootServer()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 获取服务器信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            string strComputerName = string.Empty;
            StringBuilder strTemp = new StringBuilder();
            ConnectionOptions options = new ConnectionOptions();

            if (textBox2.Text != "Computer Name or IP" && textBox2.Text.Trim() != string.Empty)
            {
                strComputerName = textBox2.Text.Trim();
            }
            else
            {
                MessageBox.Show("Please enter computer name.");
                return;
            }

            options.Username = @"ENT\MSI Drone";
            options.Password = "aaa";
            ManagementScope scope = new ManagementScope("\\\\" + strComputerName + "\\root\\cimv2", options);
            //用给定管理者用户名和口令连接远程的计算机 
            try
            {
                scope.Connect();

                //Query system for Operating System information
                ObjectQuery query = new ObjectQuery(
                    "SELECT * FROM Win32_OperatingSystem");
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher(scope, query);

                ManagementObjectCollection queryCollection = searcher.Get();
                textBox1.Text = "";
                foreach (ManagementObject m in queryCollection)
                {
                    strTemp.Append("\r\n======================\r\n");
                    // Display the remote computer information
                    strTemp.Append("Computer Name : " + m["csname"].ToString() + "\r\n");
                    strTemp.Append("Windows Directory : " + m["WindowsDirectory"].ToString() + "\r\n");
                    strTemp.Append("Operating System: " + m["Caption"].ToString() + "\r\n");
                    strTemp.Append("Version: " + m["Version"].ToString() + "\r\n");
                    strTemp.Append("Manufacturer : " + m["Manufacturer"].ToString() + "\r\n");
                    strTemp.AppendLine();
                }

                textBox1.Text = strTemp.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Connection unsuccessfully!");
                textBox1.Text = ex.Message;
            }
        }

        /// <summary>
        /// 重启服务器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            string strComputerName = string.Empty;
            StringBuilder strTemp = new StringBuilder();
            ConnectionOptions options = new ConnectionOptions();

            if (textBox2.Text != "Computer Name or IP" && textBox2.Text.Trim() != string.Empty)
            {
                strComputerName = textBox2.Text.Trim();
            }
            else
            {
                MessageBox.Show("Please enter computer name.");
                return;
            }

            options.Username = @"ENT\MSI Drone";
            options.Password = "aaa";
            ManagementScope scope = new ManagementScope("\\\\" + strComputerName + "\\root\\cimv2", options);
            try
            {
                //用给定管理者用户名和口令连接远程的计算机 
                scope.Connect();

                //Query system for Operating System information
                ObjectQuery query = new ObjectQuery(
                    "SELECT * FROM Win32_OperatingSystem");
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher(scope, query);

                ManagementObjectCollection queryCollection = searcher.Get();
                textBox1.Text = "";
                foreach (ManagementObject mo in queryCollection)
                {
                    string[] ss = { "" };
                    //重启远程计算机 
                    mo.InvokeMethod("Reboot", ss);
                }

                textBox1.Text = "Reboot successfully!";
            }
            catch (Exception ex)
            {
                MessageBox.Show("Connection unsuccessfully!");
                textBox1.Text = ex.Message;
            }
        }
    }
}
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;
namespace Test
{
    public partial class RebootServer : Form
    {
        public RebootServer()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 获取服务器信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            string strComputerName = string.Empty;
            StringBuilder strTemp = new StringBuilder();
            ConnectionOptions options = new ConnectionOptions();
            if (textBox2.Text != "Computer Name or IP" && textBox2.Text.Trim() != string.Empty)
            {
                strComputerName = textBox2.Text.Trim();
            }
            else
            {
                MessageBox.Show("Please enter computer name.");
                return;
            }
            options.Username = @"ENT\MSI Drone";
            options.Password = "aaa";
            ManagementScope scope = new ManagementScope("\\\\" + strComputerName + "\\root\\cimv2", options);
            //用给定管理者用户名和口令连接远程的计算机 
            try
            {
                scope.Connect();
                //Query system for Operating System information
                ObjectQuery query = new ObjectQuery(
                    "SELECT * FROM Win32_OperatingSystem");
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher(scope, query);
                ManagementObjectCollection queryCollection = searcher.Get();
                textBox1.Text = "";
                foreach (ManagementObject m in queryCollection)
                {
                    strTemp.Append("\r\n======================\r\n");
                    // Display the remote computer information
                    strTemp.Append("Computer Name : " + m["csname"].ToString() + "\r\n");
                    strTemp.Append("Windows Directory : " + m["WindowsDirectory"].ToString() + "\r\n");
                    strTemp.Append("Operating System: " + m["Caption"].ToString() + "\r\n");
                    strTemp.Append("Version: " + m["Version"].ToString() + "\r\n");
                    strTemp.Append("Manufacturer : " + m["Manufacturer"].ToString() + "\r\n");
                    strTemp.AppendLine();
                }
                textBox1.Text = strTemp.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Connection unsuccessfully!");
                textBox1.Text = ex.Message;
            }
        }
        /// <summary>
        /// 重启服务器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            string strComputerName = string.Empty;
            StringBuilder strTemp = new StringBuilder();
            ConnectionOptions options = new ConnectionOptions();
            if (textBox2.Text != "Computer Name or IP" && textBox2.Text.Trim() != string.Empty)
            {
                strComputerName = textBox2.Text.Trim();
            }
            else
            {
                MessageBox.Show("Please enter computer name.");
                return;
            }
            options.Username = @"ENT\MSI Drone";
            options.Password = "aaa";
            ManagementScope scope = new ManagementScope("\\\\" + strComputerName + "\\root\\cimv2", options);
            try
            {
                //用给定管理者用户名和口令连接远程的计算机 
                scope.Connect();
                //Query system for Operating System information
                ObjectQuery query = new ObjectQuery(
                    "SELECT * FROM Win32_OperatingSystem");
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher(scope, query);
                ManagementObjectCollection queryCollection = searcher.Get();
                textBox1.Text = "";
                foreach (ManagementObject mo in queryCollection)
                {
                    string[] ss = { "" };
                    //重启远程计算机 
                    mo.InvokeMethod("Reboot", ss);
                }
                textBox1.Text = "Reboot successfully!";
            }
            catch (Exception ex)
            {
                MessageBox.Show("Connection unsuccessfully!");
                textBox1.Text = ex.Message;
            }
        }
    }
}
                    
                


    
                
            
        
浙公网安备 33010602011771号