亲宝软件园·资讯

展开

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

芝麻粒儿 人气:0

实践过程

效果

代码

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 == 0)
        {
            button1.Enabled = true;
            button2.Enabled = false;
        }
        else if (FrmMain.flag == 1)
        {
            button1.Enabled = false;
            button2.Enabled = true;
            textBox1.Text = FrmMain.strName;
            textBox2.Text = FrmMain.strHost;
            textBox3.Text = FrmMain.strLPwd;
            textBox4.Text = FrmMain.strLName;
        }
    }

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

    private void button2_Click(object sender, EventArgs e)
    {
        if (textBox1.Text != FrmMain.strName)
        {
            if (database.getDs("select Name from tb_User where Name='" + textBox1.Text + "'").Tables[0].Rows.Count == 0)
            {
                if (textBox1.Text != "" && textBox2.Text != "")
                {
                    database.getCmd("update tb_User set Name='" + textBox1.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 (textBox2.Text != "")
            {
                database.getCmd("update tb_User set Host='" + textBox2.Text + "',LName='" + textBox4.Text + "',LPwd='" + textBox3.Text + "' where Name='" + FrmMain.strName + "'");
                this.Close();
            }
        }
    }

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

    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 != "")
            textBox4.Focus();
    }

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

    private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == 13 && textBox2.Text != "")
            button1.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.textBox2 = new System.Windows.Forms.TextBox();
        this.label2 = new System.Windows.Forms.Label();
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.label1 = new System.Windows.Forms.Label();
        this.button1 = new System.Windows.Forms.Button();
        this.button2 = new System.Windows.Forms.Button();
        this.button3 = new System.Windows.Forms.Button();
        this.textBox3 = new System.Windows.Forms.TextBox();
        this.label3 = new System.Windows.Forms.Label();
        this.textBox4 = new System.Windows.Forms.TextBox();
        this.label4 = new System.Windows.Forms.Label();
        this.SuspendLayout();
        // 
        // textBox2
        // 
        this.textBox2.Location = new System.Drawing.Point(77, 41);
        this.textBox2.Name = "textBox2";
        this.textBox2.Size = new System.Drawing.Size(132, 21);
        this.textBox2.TabIndex = 8;
        this.textBox2.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox2_KeyPress);
        // 
        // label2
        // 
        this.label2.AutoSize = true;
        this.label2.Location = new System.Drawing.Point(12, 44);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(65, 12);
        this.label2.TabIndex = 7;
        this.label2.Text = "对方主机:";
        // 
        // textBox1
        // 
        this.textBox1.Location = new System.Drawing.Point(78, 11);
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(132, 21);
        this.textBox1.TabIndex = 6;
        this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(13, 14);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(65, 12);
        this.label1.TabIndex = 5;
        this.label1.Text = "对方姓名:";
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(24, 129);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(54, 23);
        this.button1.TabIndex = 9;
        this.button1.Text = "添加";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // button2
        // 
        this.button2.Location = new System.Drawing.Point(84, 129);
        this.button2.Name = "button2";
        this.button2.Size = new System.Drawing.Size(54, 23);
        this.button2.TabIndex = 9;
        this.button2.Text = "修改";
        this.button2.UseVisualStyleBackColor = true;
        this.button2.Click += new System.EventHandler(this.button2_Click);
        // 
        // button3
        // 
        this.button3.Location = new System.Drawing.Point(144, 129);
        this.button3.Name = "button3";
        this.button3.Size = new System.Drawing.Size(54, 23);
        this.button3.TabIndex = 9;
        this.button3.Text = "取消";
        this.button3.UseVisualStyleBackColor = true;
        this.button3.Click += new System.EventHandler(this.button3_Click);
        // 
        // textBox3
        // 
        this.textBox3.Location = new System.Drawing.Point(76, 101);
        this.textBox3.Name = "textBox3";
        this.textBox3.PasswordChar = '*';
        this.textBox3.Size = new System.Drawing.Size(132, 21);
        this.textBox3.TabIndex = 13;
        this.textBox3.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox3_KeyPress);
        // 
        // label3
        // 
        this.label3.AutoSize = true;
        this.label3.Location = new System.Drawing.Point(11, 104);
        this.label3.Name = "label3";
        this.label3.Size = new System.Drawing.Size(65, 12);
        this.label3.TabIndex = 12;
        this.label3.Text = "登录密码:";
        // 
        // textBox4
        // 
        this.textBox4.Location = new System.Drawing.Point(77, 71);
        this.textBox4.Name = "textBox4";
        this.textBox4.Size = new System.Drawing.Size(132, 21);
        this.textBox4.TabIndex = 11;
        this.textBox4.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox4_KeyPress);
        // 
        // label4
        // 
        this.label4.AutoSize = true;
        this.label4.Location = new System.Drawing.Point(12, 74);
        this.label4.Name = "label4";
        this.label4.Size = new System.Drawing.Size(65, 12);
        this.label4.TabIndex = 10;
        this.label4.Text = "登录用户:";
        // 
        // frmEdit
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(221, 162);
        this.Controls.Add(this.textBox3);
        this.Controls.Add(this.label3);
        this.Controls.Add(this.textBox4);
        this.Controls.Add(this.label4);
        this.Controls.Add(this.button3);
        this.Controls.Add(this.button2);
        this.Controls.Add(this.button1);
        this.Controls.Add(this.textBox2);
        this.Controls.Add(this.label2);
        this.Controls.Add(this.textBox1);
        this.Controls.Add(this.label1);
        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 textBox2;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.Button button3;
    private System.Windows.Forms.TextBox textBox3;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.TextBox textBox4;
    private System.Windows.Forms.Label label4;
}
public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        public static int flag = 0;
        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 button3_Click(object sender, EventArgs e)
        {
            frmEdit frmedit = new frmEdit();
            flag = 0;
            frmedit.ShowDialog();
        }

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

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

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

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

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

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

        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();
        }

        public void UserInfo()
        {
            listBox1.DisplayMember = "Name";
            listBox1.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 = textBox4.Text;
            op.Password = textBox3.Text;
            ManagementScope scope = new ManagementScope("\\\\" + textBox2.Text + "\\root\\cimv2:Win32_Service", op);
            try
            {
                scope.Connect();
                ObjectQuery oq = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
                ManagementObjectSearcher query1 = new ManagementObjectSearcher(scope, oq);
                //得到WMI控制 
                ManagementObjectCollection queryCollection1 = query1.Get();
                foreach (ManagementObject mobj in queryCollection1)
                {
                    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.groupBox1 = new System.Windows.Forms.GroupBox();
        this.button6 = new System.Windows.Forms.Button();
        this.button5 = new System.Windows.Forms.Button();
        this.button4 = new System.Windows.Forms.Button();
        this.button3 = new System.Windows.Forms.Button();
        this.listBox1 = new System.Windows.Forms.ListBox();
        this.groupBox2 = new System.Windows.Forms.GroupBox();
        this.textBox3 = new System.Windows.Forms.TextBox();
        this.label3 = new System.Windows.Forms.Label();
        this.textBox4 = new System.Windows.Forms.TextBox();
        this.label4 = new System.Windows.Forms.Label();
        this.button2 = new System.Windows.Forms.Button();
        this.button1 = new System.Windows.Forms.Button();
        this.textBox2 = new System.Windows.Forms.TextBox();
        this.label2 = new System.Windows.Forms.Label();
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.label1 = new System.Windows.Forms.Label();
        this.groupBox1.SuspendLayout();
        this.groupBox2.SuspendLayout();
        this.SuspendLayout();
        // 
        // groupBox1
        // 
        this.groupBox1.Controls.Add(this.button6);
        this.groupBox1.Controls.Add(this.button5);
        this.groupBox1.Controls.Add(this.button4);
        this.groupBox1.Controls.Add(this.button3);
        this.groupBox1.Controls.Add(this.listBox1);
        this.groupBox1.Location = new System.Drawing.Point(8, 6);
        this.groupBox1.Name = "groupBox1";
        this.groupBox1.Size = new System.Drawing.Size(172, 194);
        this.groupBox1.TabIndex = 1;
        this.groupBox1.TabStop = false;
        this.groupBox1.Text = "人员列表";
        // 
        // button6
        // 
        this.button6.Location = new System.Drawing.Point(117, 87);
        this.button6.Name = "button6";
        this.button6.Size = new System.Drawing.Size(49, 23);
        this.button6.TabIndex = 4;
        this.button6.Text = "刷新";
        this.button6.UseVisualStyleBackColor = true;
        this.button6.Click += new System.EventHandler(this.button6_Click);
        // 
        // button5
        // 
        this.button5.Location = new System.Drawing.Point(117, 162);
        this.button5.Name = "button5";
        this.button5.Size = new System.Drawing.Size(49, 23);
        this.button5.TabIndex = 3;
        this.button5.Text = "删除";
        this.button5.UseVisualStyleBackColor = true;
        this.button5.Click += new System.EventHandler(this.button5_Click);
        // 
        // button4
        // 
        this.button4.Location = new System.Drawing.Point(117, 137);
        this.button4.Name = "button4";
        this.button4.Size = new System.Drawing.Size(49, 23);
        this.button4.TabIndex = 3;
        this.button4.Text = "修改";
        this.button4.UseVisualStyleBackColor = true;
        this.button4.Click += new System.EventHandler(this.button4_Click);
        // 
        // button3
        // 
        this.button3.Location = new System.Drawing.Point(117, 112);
        this.button3.Name = "button3";
        this.button3.Size = new System.Drawing.Size(49, 23);
        this.button3.TabIndex = 3;
        this.button3.Text = "添加";
        this.button3.UseVisualStyleBackColor = true;
        this.button3.Click += new System.EventHandler(this.button3_Click);
        // 
        // listBox1
        // 
        this.listBox1.FormattingEnabled = true;
        this.listBox1.ItemHeight = 12;
        this.listBox1.Location = new System.Drawing.Point(6, 15);
        this.listBox1.Name = "listBox1";
        this.listBox1.ScrollAlwaysVisible = true;
        this.listBox1.Size = new System.Drawing.Size(105, 172);
        this.listBox1.TabIndex = 0;
        this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
        // 
        // groupBox2
        // 
        this.groupBox2.Controls.Add(this.textBox3);
        this.groupBox2.Controls.Add(this.label3);
        this.groupBox2.Controls.Add(this.textBox4);
        this.groupBox2.Controls.Add(this.label4);
        this.groupBox2.Controls.Add(this.button2);
        this.groupBox2.Controls.Add(this.button1);
        this.groupBox2.Controls.Add(this.textBox2);
        this.groupBox2.Controls.Add(this.label2);
        this.groupBox2.Controls.Add(this.textBox1);
        this.groupBox2.Controls.Add(this.label1);
        this.groupBox2.Location = new System.Drawing.Point(186, 6);
        this.groupBox2.Name = "groupBox2";
        this.groupBox2.Size = new System.Drawing.Size(222, 194);
        this.groupBox2.TabIndex = 2;
        this.groupBox2.TabStop = false;
        this.groupBox2.Text = "远程计算机信息";
        // 
        // textBox3
        // 
        this.textBox3.Location = new System.Drawing.Point(78, 114);
        this.textBox3.Name = "textBox3";
        this.textBox3.PasswordChar = '*';
        this.textBox3.Size = new System.Drawing.Size(132, 21);
        this.textBox3.TabIndex = 8;
        // 
        // label3
        // 
        this.label3.AutoSize = true;
        this.label3.Location = new System.Drawing.Point(14, 120);
        this.label3.Name = "label3";
        this.label3.Size = new System.Drawing.Size(65, 12);
        this.label3.TabIndex = 7;
        this.label3.Text = "登录密码:";
        // 
        // textBox4
        // 
        this.textBox4.Location = new System.Drawing.Point(78, 87);
        this.textBox4.Name = "textBox4";
        this.textBox4.Size = new System.Drawing.Size(132, 21);
        this.textBox4.TabIndex = 6;
        // 
        // label4
        // 
        this.label4.AutoSize = true;
        this.label4.Location = new System.Drawing.Point(14, 90);
        this.label4.Name = "label4";
        this.label4.Size = new System.Drawing.Size(65, 12);
        this.label4.TabIndex = 5;
        this.label4.Text = "登录用户:";
        // 
        // button2
        // 
        this.button2.Location = new System.Drawing.Point(113, 148);
        this.button2.Name = "button2";
        this.button2.Size = new System.Drawing.Size(73, 31);
        this.button2.TabIndex = 4;
        this.button2.Text = "重启";
        this.button2.UseVisualStyleBackColor = true;
        this.button2.Click += new System.EventHandler(this.button2_Click);
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(33, 148);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(73, 31);
        this.button1.TabIndex = 4;
        this.button1.Text = "关闭";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // textBox2
        // 
        this.textBox2.Location = new System.Drawing.Point(78, 58);
        this.textBox2.Name = "textBox2";
        this.textBox2.Size = new System.Drawing.Size(132, 21);
        this.textBox2.TabIndex = 3;
        this.textBox2.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox2_KeyPress);
        // 
        // label2
        // 
        this.label2.AutoSize = true;
        this.label2.Location = new System.Drawing.Point(13, 61);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(65, 12);
        this.label2.TabIndex = 2;
        this.label2.Text = "对方主机:";
        // 
        // textBox1
        // 
        this.textBox1.Location = new System.Drawing.Point(79, 28);
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(132, 21);
        this.textBox1.TabIndex = 1;
        this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(14, 31);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(65, 12);
        this.label1.TabIndex = 0;
        this.label1.Text = "对方姓名:";
        // 
        // FrmMain
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(413, 204);
        this.Controls.Add(this.groupBox2);
        this.Controls.Add(this.groupBox1);
        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.groupBox1.ResumeLayout(false);
        this.groupBox2.ResumeLayout(false);
        this.groupBox2.PerformLayout();
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.GroupBox groupBox1;
    private System.Windows.Forms.ListBox listBox1;
    private System.Windows.Forms.GroupBox groupBox2;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.TextBox textBox2;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Button button5;
    private System.Windows.Forms.Button button4;
    private System.Windows.Forms.Button button3;
    private System.Windows.Forms.TextBox textBox3;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.TextBox textBox4;
    private System.Windows.Forms.Label label4;
    private System.Windows.Forms.Button button6;
}

加载全部内容

相关教程
猜你喜欢
用户评论