找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1805|回复: 0
打印 上一主题 下一主题

C#远程重启计算机代码

[复制链接]

3444

主题

3465

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
11142
跳转到指定楼层
楼主
发表于 2018-2-18 04:48:50 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

                  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 重启远程计算机
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void btn_ReBoot_Click(object sender, EventArgs e)
        {
            //定义连接远程计算机的一些选项
            ConnectionOptions options = new ConnectionOptions();
            options.Username = textBox2.Text;
            options.Password = textBox3.Text;
            ManagementScope scope = new ManagementScope("
[color=]\\\\
[color=]" + textBox1.Text + "

[color=]\\root\\cimv2
", options);
            try
            {
                //用给定管理者用户名和口令连接远程的计算机
                scope.Connect();
                ObjectQuery oq = new ObjectQuery("select * from win32_OperatingSystem");
                ManagementObjectSearcher query1 = new ManagementObjectSearcher(scope, oq);
                ManagementObjectCollection queryCollection1 = query1.Get();
                foreach (ManagementObject mo in queryCollection1)
                {
                    string[] ss ={ "" };
                    if (comboBox1.Text == "重新启动")
                    {
                        mo.InvokeMethod("Reboot", ss);
                    }
                    if (comboBox1.Text == "关闭计算机")
                    {
                        mo.InvokeMethod("Shutdown",ss);
                    }
                    //if (comboBox1.Text == "注销")
                    //{
                    //    mo.InvokeMethod("Logoff", ss);
                    //}
                }
            }
            catch (Exception er)
            {
                MessageBox.Show("连接" + textBox1.Text + "出错,出错信息为:" + er.Message);
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Add("重新启动");
            comboBox1.Items.Add("关闭计算机");
            //comboBox1.Items.Add("注销");
        }
    }
}
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

用户反馈
客户端