目录
实践过程
效果
代码
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
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
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();
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);
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 = "对方主机:";
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);
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 = "对方姓名:";
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);
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);
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);
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);
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 = "登录密码:";
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);
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 = "登录用户:";
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 关闭或重启远程计算机
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);
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
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
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();
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 = "人员列表";
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);
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);
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);
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);
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);
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 = "远程计算机信息";
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;
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 = "登录密码:";
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;
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 = "登录用户:";
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);
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);
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);
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 = "对方主机:";
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);
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 = "对方姓名:";
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;
}