C#实现远程关闭和重启计算机的示例代码

.NET
306
0
0
2023-07-13
标签   C#
目录
  • 实践过程
  • 效果
  • 代码

实践过程

效果

代码

public partial class frmEdit : Form
{
    public frmEdit()
    {
        InitializeComponent();
    }

    DataBase database = new DataBase();
    FrmMain frmmain = new FrmMain();

    private void frmEdit_Load(object sender, EventArgs e)
    {
        if (FrmMain.flag ==)
        {
            button.Enabled = true;
            button.Enabled = false;
        }
        else if (FrmMain.flag ==)
        {
            button.Enabled = false;
            button.Enabled = true;
            textBox.Text = FrmMain.strName;
            textBox.Text = FrmMain.strHost;
            textBox.Text = FrmMain.strLPwd;
            textBox.Text = FrmMain.strLName;
        }
    }

    private void button_Click(object sender, EventArgs e)
    {
        if (database.getDs("select Name from tb_User where Name='" + textBox.Text + "'").Tables[0].Rows.Count == 0)
        {
            if (textBox.Text !="" && textBox2.Text != "")
            {
                database.getCmd("insert into tb_User(Name,Host,LName,LPwd) values('" + textBox.Text + "','" + textBox2.Text + "','" + textBox4.Text + "','" + textBox3.Text + "')");
                this.Close();
            }
        }
        else
            MessageBox.Show("用户已经存在!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }

    private void button_Click(object sender, EventArgs e)
    {
        if (textBox.Text != FrmMain.strName)
        {
            if (database.getDs("select Name from tb_User where Name='" + textBox.Text + "'").Tables[0].Rows.Count == 0)
            {
                if (textBox.Text != "" && textBox2.Text != "")
                {
                    database.getCmd("update tb_User set Name='" + textBox.Text + "',Host='" + textBox2.Text + "',LName='" + textBox4.Text + "',LPwd='" + textBox3.Text + "' where Name='" + FrmMain.strName + "'");
                    this.Close();
                }
            }
            else
                MessageBox.Show("用户已经存在!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        else
        {
            if (textBox.Text != "")
            {
                database.getCmd("update tb_User set Host='" + textBox.Text + "',LName='" + textBox4.Text + "',LPwd='" + textBox3.Text + "' where Name='" + FrmMain.strName + "'");
                this.Close();
            }
        }
    }

    private void button_Click(object sender, EventArgs e)
    {
        this.Close();
    }

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

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

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

    private void textBox_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == && textBox2.Text != "")
            button.Focus();
    }
}
partial class frmEdit
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.textBox = new System.Windows.Forms.TextBox();
        this.label = new System.Windows.Forms.Label();
        this.textBox = new System.Windows.Forms.TextBox();
        this.label = new System.Windows.Forms.Label();
        this.button = new System.Windows.Forms.Button();
        this.button = new System.Windows.Forms.Button();
        this.button = new System.Windows.Forms.Button();
        this.textBox = new System.Windows.Forms.TextBox();
        this.label = new System.Windows.Forms.Label();
        this.textBox = new System.Windows.Forms.TextBox();
        this.label = new System.Windows.Forms.Label();
        this.SuspendLayout();
        // 
        // textBox
        // 
        this.textBox.Location = new System.Drawing.Point(77, 41);
        this.textBox.Name = "textBox2";
        this.textBox.Size = new System.Drawing.Size(132, 21);
        this.textBox.TabIndex = 8;
        this.textBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox2_KeyPress);
        // 
        // label
        // 
        this.label.AutoSize = true;
        this.label.Location = new System.Drawing.Point(12, 44);
        this.label.Name = "label2";
        this.label.Size = new System.Drawing.Size(65, 12);
        this.label.TabIndex = 7;
        this.label.Text = "对方主机:";
        // 
        // textBox
        // 
        this.textBox.Location = new System.Drawing.Point(78, 11);
        this.textBox.Name = "textBox1";
        this.textBox.Size = new System.Drawing.Size(132, 21);
        this.textBox.TabIndex = 6;
        this.textBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
        // 
        // label
        // 
        this.label.AutoSize = true;
        this.label.Location = new System.Drawing.Point(13, 14);
        this.label.Name = "label1";
        this.label.Size = new System.Drawing.Size(65, 12);
        this.label.TabIndex = 5;
        this.label.Text = "对方姓名:";
        // 
        // button
        // 
        this.button.Location = new System.Drawing.Point(24, 129);
        this.button.Name = "button1";
        this.button.Size = new System.Drawing.Size(54, 23);
        this.button.TabIndex = 9;
        this.button.Text = "添加";
        this.button.UseVisualStyleBackColor = true;
        this.button.Click += new System.EventHandler(this.button1_Click);
        // 
        // button
        // 
        this.button.Location = new System.Drawing.Point(84, 129);
        this.button.Name = "button2";
        this.button.Size = new System.Drawing.Size(54, 23);
        this.button.TabIndex = 9;
        this.button.Text = "修改";
        this.button.UseVisualStyleBackColor = true;
        this.button.Click += new System.EventHandler(this.button2_Click);
        // 
        // button
        // 
        this.button.Location = new System.Drawing.Point(144, 129);
        this.button.Name = "button3";
        this.button.Size = new System.Drawing.Size(54, 23);
        this.button.TabIndex = 9;
        this.button.Text = "取消";
        this.button.UseVisualStyleBackColor = true;
        this.button.Click += new System.EventHandler(this.button3_Click);
        // 
        // textBox
        // 
        this.textBox.Location = new System.Drawing.Point(76, 101);
        this.textBox.Name = "textBox3";
        this.textBox.PasswordChar = '*';
        this.textBox.Size = new System.Drawing.Size(132, 21);
        this.textBox.TabIndex = 13;
        this.textBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox3_KeyPress);
        // 
        // label
        // 
        this.label.AutoSize = true;
        this.label.Location = new System.Drawing.Point(11, 104);
        this.label.Name = "label3";
        this.label.Size = new System.Drawing.Size(65, 12);
        this.label.TabIndex = 12;
        this.label.Text = "登录密码:";
        // 
        // textBox
        // 
        this.textBox.Location = new System.Drawing.Point(77, 71);
        this.textBox.Name = "textBox4";
        this.textBox.Size = new System.Drawing.Size(132, 21);
        this.textBox.TabIndex = 11;
        this.textBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox4_KeyPress);
        // 
        // label
        // 
        this.label.AutoSize = true;
        this.label.Location = new System.Drawing.Point(12, 74);
        this.label.Name = "label4";
        this.label.Size = new System.Drawing.Size(65, 12);
        this.label.TabIndex = 10;
        this.label.Text = "登录用户:";
        // 
        // frmEdit
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(F, 12F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(, 162);
        this.Controls.Add(this.textBox);
        this.Controls.Add(this.label);
        this.Controls.Add(this.textBox);
        this.Controls.Add(this.label);
        this.Controls.Add(this.button);
        this.Controls.Add(this.button);
        this.Controls.Add(this.button);
        this.Controls.Add(this.textBox);
        this.Controls.Add(this.label);
        this.Controls.Add(this.textBox);
        this.Controls.Add(this.label);
        this.MaximizeBox = false;
        this.MinimizeBox = false;
        this.Name = "frmEdit";
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.Text = "添加/修改人员信息";
        this.Load += new System.EventHandler(this.frmEdit_Load);
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.TextBox textBox;
    private System.Windows.Forms.Label label;
    private System.Windows.Forms.TextBox textBox;
    private System.Windows.Forms.Label label;
    private System.Windows.Forms.Button button;
    private System.Windows.Forms.Button button;
    private System.Windows.Forms.Button button;
    private System.Windows.Forms.TextBox textBox;
    private System.Windows.Forms.Label label;
    private System.Windows.Forms.TextBox textBox;
    private System.Windows.Forms.Label label;
}
public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        public static int flag =;
        public static string strName = "";
        public static string strHost = "";
        public static string strLName = "";
        public static string strLPwd = "";
        public bool blFlag = false;
        DataBase database = new DataBase();

        private void FrmMain_Load(object sender, EventArgs e)
        {
            UserInfo();
        }

        private void button_Click(object sender, EventArgs e)
        {
            frmEdit frmedit = new frmEdit();
            flag =;
            frmedit.ShowDialog();
        }

        private void button_Click(object sender, EventArgs e)
        {
            if (listBox.SelectedIndices.Count > 0)
            {
                frmEdit frmedit = new frmEdit();
                flag =;
                strName = listBox.Text;
                DataSet myds = database.getDs("select * from tb_User where Name='" + strName + "'");
                strHost = myds.Tables[].Rows[0][1].ToString();
                strLName = myds.Tables[].Rows[0][2].ToString();
                strLPwd = myds.Tables[].Rows[0][3].ToString();
                frmedit.ShowDialog();
            }
        }

        private void button_Click(object sender, EventArgs e)
        {
            if (listBox.SelectedIndices.Count > 0)
                database.getCmd("delete from tb_User where Name='" + strName + "'");
            UserInfo();
        }

        private void listBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox.Text = listBox1.Text;
            strName = listBox.Text;
            textBox.Text = database.getDs("select * from tb_User where Name='" + listBox1.Text + "'").Tables[0].Rows[0][1].ToString();
            textBox.Text = database.getDs("select * from tb_User where Name='" + listBox1.Text + "'").Tables[0].Rows[0][2].ToString();
            textBox.Text = database.getDs("select * from tb_User where Name='" + listBox1.Text + "'").Tables[0].Rows[0][3].ToString();
        }

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

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

        private void button_Click(object sender, EventArgs e)
        {
            UserInfo();
        }

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

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

        public void UserInfo()
        {
            listBox.DisplayMember = "Name";
            listBox.DataSource = database.getDs("select Name from tb_User").Tables[0];
        }

        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();
            op.Username = textBox.Text;
            op.Password = textBox.Text;
            ManagementScope scope = new ManagementScope("\\\\" + textBox.Text + "\\root\\cimv2:Win32_Service", op);
            try
            {
                scope.Connect();
                ObjectQuery oq = new ObjectQuery("SELECT * FROM Win_OperatingSystem");
                ManagementObjectSearcher query = new ManagementObjectSearcher(scope, oq);
                //得到WMI控制 
                ManagementObjectCollection queryCollection = query1.Get();
                foreach (ManagementObject mobj in queryCollection)
                {
                    string[] str = { "" };
                    mobj.InvokeMethod(doinfo, str);
                }
            }
            catch (Exception ey)
            {
                MessageBox.Show(ey.Message);
            }
        }
        #endregion
    }
partial class FrmMain
{
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows 窗体设计器生成的代码

    /// <summary>
    /// 设计器支持所需的方法 - 不要
    /// 使用代码编辑器修改此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
        this.groupBox = new System.Windows.Forms.GroupBox();
        this.button = new System.Windows.Forms.Button();
        this.button = new System.Windows.Forms.Button();
        this.button = new System.Windows.Forms.Button();
        this.button = new System.Windows.Forms.Button();
        this.listBox = new System.Windows.Forms.ListBox();
        this.groupBox = new System.Windows.Forms.GroupBox();
        this.textBox = new System.Windows.Forms.TextBox();
        this.label = new System.Windows.Forms.Label();
        this.textBox = new System.Windows.Forms.TextBox();
        this.label = new System.Windows.Forms.Label();
        this.button = new System.Windows.Forms.Button();
        this.button = new System.Windows.Forms.Button();
        this.textBox = new System.Windows.Forms.TextBox();
        this.label = new System.Windows.Forms.Label();
        this.textBox = new System.Windows.Forms.TextBox();
        this.label = new System.Windows.Forms.Label();
        this.groupBox.SuspendLayout();
        this.groupBox.SuspendLayout();
        this.SuspendLayout();
        // 
        // groupBox
        // 
        this.groupBox.Controls.Add(this.button6);
        this.groupBox.Controls.Add(this.button5);
        this.groupBox.Controls.Add(this.button4);
        this.groupBox.Controls.Add(this.button3);
        this.groupBox.Controls.Add(this.listBox1);
        this.groupBox.Location = new System.Drawing.Point(8, 6);
        this.groupBox.Name = "groupBox1";
        this.groupBox.Size = new System.Drawing.Size(172, 194);
        this.groupBox.TabIndex = 1;
        this.groupBox.TabStop = false;
        this.groupBox.Text = "人员列表";
        // 
        // button
        // 
        this.button.Location = new System.Drawing.Point(117, 87);
        this.button.Name = "button6";
        this.button.Size = new System.Drawing.Size(49, 23);
        this.button.TabIndex = 4;
        this.button.Text = "刷新";
        this.button.UseVisualStyleBackColor = true;
        this.button.Click += new System.EventHandler(this.button6_Click);
        // 
        // button
        // 
        this.button.Location = new System.Drawing.Point(117, 162);
        this.button.Name = "button5";
        this.button.Size = new System.Drawing.Size(49, 23);
        this.button.TabIndex = 3;
        this.button.Text = "删除";
        this.button.UseVisualStyleBackColor = true;
        this.button.Click += new System.EventHandler(this.button5_Click);
        // 
        // button
        // 
        this.button.Location = new System.Drawing.Point(117, 137);
        this.button.Name = "button4";
        this.button.Size = new System.Drawing.Size(49, 23);
        this.button.TabIndex = 3;
        this.button.Text = "修改";
        this.button.UseVisualStyleBackColor = true;
        this.button.Click += new System.EventHandler(this.button4_Click);
        // 
        // button
        // 
        this.button.Location = new System.Drawing.Point(117, 112);
        this.button.Name = "button3";
        this.button.Size = new System.Drawing.Size(49, 23);
        this.button.TabIndex = 3;
        this.button.Text = "添加";
        this.button.UseVisualStyleBackColor = true;
        this.button.Click += new System.EventHandler(this.button3_Click);
        // 
        // listBox
        // 
        this.listBox.FormattingEnabled = true;
        this.listBox.ItemHeight = 12;
        this.listBox.Location = new System.Drawing.Point(6, 15);
        this.listBox.Name = "listBox1";
        this.listBox.ScrollAlwaysVisible = true;
        this.listBox.Size = new System.Drawing.Size(105, 172);
        this.listBox.TabIndex = 0;
        this.listBox.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
        // 
        // groupBox
        // 
        this.groupBox.Controls.Add(this.textBox3);
        this.groupBox.Controls.Add(this.label3);
        this.groupBox.Controls.Add(this.textBox4);
        this.groupBox.Controls.Add(this.label4);
        this.groupBox.Controls.Add(this.button2);
        this.groupBox.Controls.Add(this.button1);
        this.groupBox.Controls.Add(this.textBox2);
        this.groupBox.Controls.Add(this.label2);
        this.groupBox.Controls.Add(this.textBox1);
        this.groupBox.Controls.Add(this.label1);
        this.groupBox.Location = new System.Drawing.Point(186, 6);
        this.groupBox.Name = "groupBox2";
        this.groupBox.Size = new System.Drawing.Size(222, 194);
        this.groupBox.TabIndex = 2;
        this.groupBox.TabStop = false;
        this.groupBox.Text = "远程计算机信息";
        // 
        // textBox
        // 
        this.textBox.Location = new System.Drawing.Point(78, 114);
        this.textBox.Name = "textBox3";
        this.textBox.PasswordChar = '*';
        this.textBox.Size = new System.Drawing.Size(132, 21);
        this.textBox.TabIndex = 8;
        // 
        // label
        // 
        this.label.AutoSize = true;
        this.label.Location = new System.Drawing.Point(14, 120);
        this.label.Name = "label3";
        this.label.Size = new System.Drawing.Size(65, 12);
        this.label.TabIndex = 7;
        this.label.Text = "登录密码:";
        // 
        // textBox
        // 
        this.textBox.Location = new System.Drawing.Point(78, 87);
        this.textBox.Name = "textBox4";
        this.textBox.Size = new System.Drawing.Size(132, 21);
        this.textBox.TabIndex = 6;
        // 
        // label
        // 
        this.label.AutoSize = true;
        this.label.Location = new System.Drawing.Point(14, 90);
        this.label.Name = "label4";
        this.label.Size = new System.Drawing.Size(65, 12);
        this.label.TabIndex = 5;
        this.label.Text = "登录用户:";
        // 
        // button
        // 
        this.button.Location = new System.Drawing.Point(113, 148);
        this.button.Name = "button2";
        this.button.Size = new System.Drawing.Size(73, 31);
        this.button.TabIndex = 4;
        this.button.Text = "重启";
        this.button.UseVisualStyleBackColor = true;
        this.button.Click += new System.EventHandler(this.button2_Click);
        // 
        // button
        // 
        this.button.Location = new System.Drawing.Point(33, 148);
        this.button.Name = "button1";
        this.button.Size = new System.Drawing.Size(73, 31);
        this.button.TabIndex = 4;
        this.button.Text = "关闭";
        this.button.UseVisualStyleBackColor = true;
        this.button.Click += new System.EventHandler(this.button1_Click);
        // 
        // textBox
        // 
        this.textBox.Location = new System.Drawing.Point(78, 58);
        this.textBox.Name = "textBox2";
        this.textBox.Size = new System.Drawing.Size(132, 21);
        this.textBox.TabIndex = 3;
        this.textBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox2_KeyPress);
        // 
        // label
        // 
        this.label.AutoSize = true;
        this.label.Location = new System.Drawing.Point(13, 61);
        this.label.Name = "label2";
        this.label.Size = new System.Drawing.Size(65, 12);
        this.label.TabIndex = 2;
        this.label.Text = "对方主机:";
        // 
        // textBox
        // 
        this.textBox.Location = new System.Drawing.Point(79, 28);
        this.textBox.Name = "textBox1";
        this.textBox.Size = new System.Drawing.Size(132, 21);
        this.textBox.TabIndex = 1;
        this.textBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
        // 
        // label
        // 
        this.label.AutoSize = true;
        this.label.Location = new System.Drawing.Point(14, 31);
        this.label.Name = "label1";
        this.label.Size = new System.Drawing.Size(65, 12);
        this.label.TabIndex = 0;
        this.label.Text = "对方姓名:";
        // 
        // FrmMain
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(F, 12F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(, 204);
        this.Controls.Add(this.groupBox);
        this.Controls.Add(this.groupBox);
        this.Name = "FrmMain";
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.Text = "远程关闭与重启计算机";
        this.Load += new System.EventHandler(this.FrmMain_Load);
        this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.FrmMain_FormClosed);
        this.groupBox.ResumeLayout(false);
        this.groupBox.ResumeLayout(false);
        this.groupBox.PerformLayout();
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.GroupBox groupBox;
    private System.Windows.Forms.ListBox listBox;
    private System.Windows.Forms.GroupBox groupBox;
    private System.Windows.Forms.Button button;
    private System.Windows.Forms.Button button;
    private System.Windows.Forms.TextBox textBox;
    private System.Windows.Forms.Label label;
    private System.Windows.Forms.TextBox textBox;
    private System.Windows.Forms.Label label;
    private System.Windows.Forms.Button button;
    private System.Windows.Forms.Button button;
    private System.Windows.Forms.Button button;
    private System.Windows.Forms.TextBox textBox;
    private System.Windows.Forms.Label label;
    private System.Windows.Forms.TextBox textBox;
    private System.Windows.Forms.Label label;
    private System.Windows.Forms.Button button;
}