Loading
0

c#实现远程重启及远程关机的方法

技术小学生微信公众号
腾讯云服务器大促销。
华为服务器
前言:c#实现远程关闭计算机及远程重启某电脑的方法,如何在c#中实现远程重启电脑及远程关机方法

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Management;

namespace CRRemoteComputer
{
    public partial class Frm_Main : Form
    {
        public Frm_Main()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CloseComputer("Shutdown");//远程关闭计算机
        }

        private void button2_Click(object sender, EventArgs e)
        {
            CloseComputer("Reboot");//远程重启计算机
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13 && textBox1.Text != "")
                textBox2.Focus();
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13 && textBox2.Text != "")
                button1.Focus();
        }

        private void FrmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        #region 关闭或重启远程计算机
        /// <summary>
        /// 关闭或重启远程计算机
        /// </summary>
        /// <param name="doinfo">要执行的操作命令</param>
        private void CloseComputer(string doinfo)
        {
            ConnectionOptions op = new ConnectionOptions();//创建ConnectionOptions对象
            op.Username = textBox4.Text;//设置远程机器用户名
            op.Password = textBox3.Text;//设置远程机器登录密码
            //创建ManagementScope对象
            ManagementScope scope = new ManagementScope("\\\\" + textBox2.Text + "\\root\\cimv2:Win32_Service", op);
            try
            {
                scope.Connect();//连接远程对象
                ObjectQuery oq = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");//实例化ObjectQuery对象
                //创建ManagementObjectSearcher对象
                ManagementObjectSearcher query1 = new ManagementObjectSearcher(scope, oq);
                ManagementObjectCollection queryCollection1 = query1.Get();//得到WMI控制
                foreach (ManagementObject mobj in queryCollection1)
                {
                    string[] str = { "" };
                    mobj.InvokeMethod(doinfo, str);
                }
            }
            catch (Exception ey)
            {
                MessageBox.Show(ey.Message);
            }
        }
        #endregion
    }
}



 
技术小学生微信公众号
华为服务器
腾讯云服务器大促销。

声明:站长码字很辛苦啊,转载时请保留本声明及附带文章链接:https://blog.tag.gg/showinfo-23-35937-0.html
亲爱的:若该文章解决了您的问题,可否收藏+评论+分享呢?
上一篇:c#实现定时关闭电脑/计算机/系统的方法
下一篇:c#中图形显示磁盘的使用量及未使用量方法