简单地获取WIN32_Account类的属性。
需要添加System.Management的引用。
由于涉及到安全的问题,运行结果就不截图了。。。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Management.Instrumentation;
using System.Management;
namespace Eyuan.WMIStudy.Win32
{
public partial class fmAccount : Form
{
public fmAccount()
{
InitializeComponent();
}
StringBuilder sbAccountInfo = new StringBuilder();
string strSql = string.Empty;
int iMOCount;
private void fmAccount_Load(object sender, EventArgs e)
{
strSql = "SELECT * FROM Win32_Account";
ManagementObjectSearcher search = new ManagementObjectSearcher(strSql);
iMOCount = 0;
foreach (ManagementObject mo in search.Get())
{
sbAccountInfo.AppendLine("第" + (++iMOCount) + "个Account:");
//sbAccountInfo.AppendLine("Caption:" + mo["Caption"]);
sbAccountInfo.AppendFormat("{0,-15}:{1}\n", "Caption", mo["Caption"]);
//sbAccountInfo.AppendLine("Description:" + mo["Description"]);
sbAccountInfo.AppendFormat("{0,-15}:{1}\n", "Description", mo["Description"]);
//sbAccountInfo.AppendLine("Domain:" + mo["Domain"]);
sbAccountInfo.AppendFormat("{0,-15}:{1}\n", "Domain", mo["Domain"]);
//sbAccountInfo.AppendLine("InstallDate:" + mo["InstallDate"]);
sbAccountInfo.AppendFormat("{0,-15}:{1}\n", "InstallDate", mo["InstallDate"]);
//sbAccountInfo.AppendLine("LocalAccount:" + mo["LocalAccount"]);
sbAccountInfo.AppendFormat("{0,-15}:{1}\n", "LocalAccount", mo["LocalAccount"]);
//sbAccountInfo.AppendLine("Name:" + mo["Name"]);
sbAccountInfo.AppendFormat("{0,-15}:{1}\n", "Name", mo["Name"]);
//sbAccountInfo.AppendLine("SID:" + mo["SID"]);
sbAccountInfo.AppendFormat("{0,-15}:{1}\n", "SID", mo["SID"]);
//sbAccountInfo.AppendLine("SIDType:" + mo["SIDType"]);
sbAccountInfo.AppendFormat("{0,-15}:{1}\n", "SIDType", mo["SIDType"]);
//sbAccountInfo.AppendLine("Status:" + mo["Status"]);
sbAccountInfo.AppendFormat("{0,-15}:{1}\n", "Status", mo["Status"]);
sbAccountInfo.AppendLine("");
}
//MessageBox.Show(sbAccountInfo.ToString());
lbAccountInfo.Items.Clear();
foreach (string strAccountInfo in sbAccountInfo.ToString().Split(new string []{"\n"},StringSplitOptions.None))
{
lbAccountInfo.Items.Add(strAccountInfo);
}
}
}
}