📄 systeminfocontrol.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Management;
using System.Globalization;
namespace WindowsInfoControlLibrary
{
/// <summary>
/// This UserControl display system information.
/// </summary>
public class SysInfoControl : System.Windows.Forms.UserControl
{
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.RadioButton radioMachine;
private System.Windows.Forms.TextBox textIP;
private System.Windows.Forms.Label labelUserID;
private System.Windows.Forms.Label labelPassword;
private System.Windows.Forms.TextBox textUserID;
private System.Windows.Forms.TextBox textPassword;
private System.Windows.Forms.TreeView treeSysInfo;
private System.Windows.Forms.Label labelIP;
private System.Windows.Forms.RadioButton radioRemoteMachine;
private System.Windows.Forms.Button buttonRefresh;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private bool boolLoaded = false;
//delegate status event
public delegate void Status(string e);
public event Status UpdateStatus;
public SysInfoControl()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Component 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.panel1 = new System.Windows.Forms.Panel();
this.buttonRefresh = new System.Windows.Forms.Button();
this.radioRemoteMachine = new System.Windows.Forms.RadioButton();
this.textPassword = new System.Windows.Forms.TextBox();
this.textUserID = new System.Windows.Forms.TextBox();
this.labelPassword = new System.Windows.Forms.Label();
this.labelUserID = new System.Windows.Forms.Label();
this.labelIP = new System.Windows.Forms.Label();
this.textIP = new System.Windows.Forms.TextBox();
this.radioMachine = new System.Windows.Forms.RadioButton();
this.treeSysInfo = new System.Windows.Forms.TreeView();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.buttonRefresh,
this.radioRemoteMachine,
this.textPassword,
this.textUserID,
this.labelPassword,
this.labelUserID,
this.labelIP,
this.textIP,
this.radioMachine});
this.panel1.Dock = System.Windows.Forms.DockStyle.Right;
this.panel1.Location = new System.Drawing.Point(488, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(128, 432);
this.panel1.TabIndex = 9;
//
// buttonRefresh
//
this.buttonRefresh.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.buttonRefresh.Location = new System.Drawing.Point(29, 15);
this.buttonRefresh.Name = "buttonRefresh";
this.buttonRefresh.Size = new System.Drawing.Size(75, 24);
this.buttonRefresh.TabIndex = 1;
this.buttonRefresh.Text = "&Refresh";
this.buttonRefresh.Click += new System.EventHandler(this.buttonRefresh_Click);
//
// radioRemoteMachine
//
this.radioRemoteMachine.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.radioRemoteMachine.Location = new System.Drawing.Point(13, 82);
this.radioRemoteMachine.Name = "radioRemoteMachine";
this.radioRemoteMachine.Size = new System.Drawing.Size(120, 24);
this.radioRemoteMachine.TabIndex = 3;
this.radioRemoteMachine.Text = "&Remote Machine";
this.radioRemoteMachine.CheckedChanged += new System.EventHandler(this.radioMachine_CheckedChanged);
//
// textPassword
//
this.textPassword.Location = new System.Drawing.Point(12, 270);
this.textPassword.Name = "textPassword";
this.textPassword.PasswordChar = '*';
this.textPassword.Size = new System.Drawing.Size(104, 20);
this.textPassword.TabIndex = 9;
this.textPassword.Text = "";
this.textPassword.Visible = false;
//
// textUserID
//
this.textUserID.Location = new System.Drawing.Point(12, 209);
this.textUserID.Name = "textUserID";
this.textUserID.Size = new System.Drawing.Size(104, 20);
this.textUserID.TabIndex = 7;
this.textUserID.Text = "";
this.textUserID.Visible = false;
//
// labelPassword
//
this.labelPassword.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.labelPassword.Location = new System.Drawing.Point(12, 245);
this.labelPassword.Name = "labelPassword";
this.labelPassword.Size = new System.Drawing.Size(100, 16);
this.labelPassword.TabIndex = 8;
this.labelPassword.Text = "&Password:";
this.labelPassword.Visible = false;
//
// labelUserID
//
this.labelUserID.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.labelUserID.Location = new System.Drawing.Point(12, 185);
this.labelUserID.Name = "labelUserID";
this.labelUserID.Size = new System.Drawing.Size(100, 16);
this.labelUserID.TabIndex = 6;
this.labelUserID.Text = "&User ID:";
this.labelUserID.Visible = false;
//
// labelIP
//
this.labelIP.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.labelIP.Location = new System.Drawing.Point(12, 123);
this.labelIP.Name = "labelIP";
this.labelIP.Size = new System.Drawing.Size(112, 16);
this.labelIP.TabIndex = 4;
this.labelIP.Text = "&IP or Machine Name:";
this.labelIP.Visible = false;
//
// textIP
//
this.textIP.Location = new System.Drawing.Point(12, 148);
this.textIP.Name = "textIP";
this.textIP.Size = new System.Drawing.Size(104, 20);
this.textIP.TabIndex = 5;
this.textIP.Text = "";
this.textIP.Visible = false;
//
// radioMachine
//
this.radioMachine.Checked = true;
this.radioMachine.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.radioMachine.Location = new System.Drawing.Point(12, 54);
this.radioMachine.Name = "radioMachine";
this.radioMachine.Size = new System.Drawing.Size(120, 24);
this.radioMachine.TabIndex = 2;
this.radioMachine.TabStop = true;
this.radioMachine.Text = "&Current Machine";
this.radioMachine.CheckedChanged += new System.EventHandler(this.radioMachine_CheckedChanged);
//
// treeSysInfo
//
this.treeSysInfo.Dock = System.Windows.Forms.DockStyle.Fill;
this.treeSysInfo.ImageIndex = -1;
this.treeSysInfo.Name = "treeSysInfo";
this.treeSysInfo.SelectedImageIndex = -1;
this.treeSysInfo.Size = new System.Drawing.Size(488, 432);
this.treeSysInfo.TabIndex = 0;
//
// SysInfoControl
//
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.treeSysInfo,
this.panel1});
this.Name = "SysInfoControl";
this.Size = new System.Drawing.Size(616, 432);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// Overide Refresh function to requery system information
/// </summary>
public override void Refresh()
{
//set curser to hour glass
this.Cursor = Cursors.WaitCursor;
//clear text box
treeSysInfo.Nodes.Clear();
//get system info
getSystemInfo();
//set curser to hour glass
this.Cursor = Cursors.Default;
}
/// <summary>
/// Loaded property
/// </summary>
/// <returns>boolLoaded</returns>
public bool Loaded
{
get
{
//true if control already loaded else false.
return boolLoaded;
}
}
/// <summary>
/// Private property
/// </summary>
/// <param name="booleanFlag"></param>
private void setLoaded(bool booleanFlag)
{
boolLoaded = booleanFlag;
}
/// <summary>
/// Get system information
/// </summary>
private void getSystemInfo()
{
ManagementObjectSearcher query;
ManagementObjectCollection queryCollection;
System.Management.ObjectQuery oq;
TreeNodeCollection nodeCollection;
string stringMachineName = "";
//Connect to the remote computer
ConnectionOptions co = new ConnectionOptions();
if (radioMachine.Checked == true)
{
stringMachineName = "localhost";
}
else
{
stringMachineName = textIP.Text;
}
if (stringMachineName.Trim().Length == 0)
{
MessageBox.Show("Must enter machine IP address or name.");
return;
}
//get user and password
if (textUserID.Text.Trim().Length > 0)
{
co.Username = textUserID.Text;
co.Password = textPassword.Text;
}
//Point to machine
System.Management.ManagementScope ms = new System.Management.ManagementScope("\\\\" + stringMachineName + "\\root\\cimv2", co);
//Status
updateStatus("Getting System Information");
//Query remote computer across the connection
oq = new System.Management.ObjectQuery("SELECT * FROM Win32_OperatingSystem");
query = new ManagementObjectSearcher(ms,oq);
try
{
//clear TreeView
treeSysInfo.Nodes.Clear();
//create parent
nodeCollection = createParentNode("Operating System");
queryCollection = query.Get();
foreach ( ManagementObject mo in queryCollection)
{
//create child node for operating system
createChildNode(nodeCollection, "Operating System: " + mo["Caption"]);
createChildNode(nodeCollection, "Version: " + mo["Version"]);
createChildNode(nodeCollection, "Manufacturer : " + mo["Manufacturer"]);
createChildNode(nodeCollection, "Computer Name : " +mo["csname"]);
createChildNode(nodeCollection, "Windows Directory : " + mo["WindowsDirectory"]);
createChildNode(nodeCollection, "Serial Number : " + mo["SerialNumber"]);
}
this.Update();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -