C#实现远程关机的小程序

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 Ex18_11
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //指定生成 WMI 连接所需的所有设置
            ConnectionOptions op = new ConnectionOptions();
            op.Username = "st6022";  //远程计算机用户名称
            op.Password = "administrator";   //远程计算机用户密码
            //设置操作管理范围
            ManagementScope scope = new ManagementScope("\\" + "10.10.10.89" + "\root\cimv2", op);
            scope.Connect();  //将此 ManagementScope 连接到实际的 WMI 范围。
            ObjectQuery oq = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
            ManagementObjectSearcher query = new ManagementObjectSearcher(scope, oq);
            //得到WMI控制 
            ManagementObjectCollection queryCollection = query.Get();
            foreach (ManagementObject obj in queryCollection)
            {
                obj.InvokeMethod("ShutDown", null); //执行关闭远程计算机,reboot为重新启动
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Ex18_11
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
原文地址:https://www.cnblogs.com/xiaoqiaccp/p/3140444.html