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;
using Microsoft.Win32;
using System.DirectoryServices;
using System.DirectoryServices.Protocols;
using System.Collections;
namespace WMI
{
public partial class Form1 : Form,ICollection
{
public Form1()
{
InitializeComponent();
_list = new string[] { "You", "and", "me", "!" };
}
private string[] _list;
private object _root = new object();
#region ICollection Members
public void CopyTo(Array array, int index)
{
_list.CopyTo(array, index);
}
public int Count
{
get { return _list.Length; }
}
public bool IsSynchronized
{
get { return true; }
}
public object SyncRoot
{
get { return _root; }
}
#endregion
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return _list.GetEnumerator();
}
#endregion
/// <summary>
/// 修改本地密码的方法
/// </summary>
/// <param name="intputPwd">输入的新密码</param>
/// <returns>成功返回"success",失败返回exception</returns>
public static string UpdatePassWord(string intputPwd)
{
try
{
DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
DirectoryEntry user = AD.Children.Find("Guest", "User");
user.Invoke("SetPassword", new object[] { intputPwd });
return "success";
}
catch (Exception ex)
{
return ex.ToString();
}
}
/// <summary>
/// 根据本地用户组获得组里的用户名数组
/// </summary>
/// <param name="localGroup">本地用户组</param>
/// <returns>用户名数组</returns>
static ArrayList GetUsersArrayList(DirectoryEntry directoryEntry)
{
ArrayList arrUsers = new ArrayList();
try
{
foreach (object member in (IEnumerable)directoryEntry.Invoke("Members"))
{
DirectoryEntry dirmem = new DirectoryEntry(member);
arrUsers.Add(dirmem.Name);
}
return arrUsers;
}
catch { return arrUsers; }
}
/// <summary>
/// 获得每个单独的用户信息
/// </summary>
/// <param name="userName">用户名</param>
/// <param name="directoryEntry">目录入口</param>
/// <returns>单独用户信息的HashTable</returns>
static Hashtable GetSingleUserInfo(string userName, string localGroup)
{
Hashtable ht = new Hashtable(); //HASHTABLE
try
{
DirectoryEntry group = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
try
{
System.DirectoryServices.DirectoryEntry user = group.Children.Find(userName, "User");
string FullName = Convert.ToString(user.Invoke("Get", new object[] { "FullName" }));
string Description = Convert.ToString(user.Invoke("Get", new object[] { "Description" }));
string[] ok1 = new string[24];
string ok="";
user.Properties.PropertyNames.CopyTo(ok1,0);
for (int i = 0; i < user.Properties.Count; i++)
{
ht.Add(ok1[i], user.Properties[ok1[i]].Value.ToString());
}
//ok += ok1[i]+"\n";
//ht.Add("Username", userName);
//ht.Add("FullName", FullName);
//ht.Add("Description", Description);
// ht.Add("ok", ok);
}
catch { return ht; };
}
catch { }
return ht;
}
/// <summary>
/// 根据用户组,查询本地包含用户HashTable(含名称、全名、描述)的数组
/// </summary>
/// <param name="localGroup">用户组名称</param>
/// <returns>包含用户HashTable(含名称、全名、描述)的数组</returns>
public static ArrayList GetUserInfoArrayList(string localGroup)
{
ArrayList arr = new ArrayList();//al返回HASHTABLE数组用
ArrayList arrReviceUser = new ArrayList();//reviceUser接受用户数组用
try
{
DirectoryEntry group = new DirectoryEntry("WinNT://" + Environment.MachineName + "/" + localGroup + ",group");
arrReviceUser = GetUsersArrayList(group);
foreach (string user in arrReviceUser)
{
arr.Add(GetSingleUserInfo(user, localGroup));
}
}
catch (Exception ex)
{
string errMsg = ex.ToString();
}
return arr;
}
private void button1_Click(object sender, EventArgs e)
{
//MyWMI = new ManagementClass("Win32_QuickFixEngineering");
//Application.DoEvents();
// try
//{
//foreach (ManagementObject mo in MyWMI.GetInstances())
// richTextBox1.Text += mo.GetPropertyValue("InstalledOn")+"\n";
// }
//catch { }
//var queryKey = new SelectQuery("SELECT InstalledOn FROM Win32_QuickFixEngineering ");
//var queryResults = new ManagementObjectSearcher(queryKey);
//foreach (ManagementObject m in queryResults.Get())
//{
// richTextBox1.Text += m.GetProtyValue("InstalledOn") + "\n";
// }
Hashtable s = GetSingleUserInfo("Administrator","Administrators");
//this.richTextBox1.Text += s["Username"] + "\n";
// this.richTextBox1.Text += s["Description"] + "\n";
//this.richTextBox1.Text += s["ok"] + "\n";
string[] hello = new string[24];
string[] hello1 = new string[24];
s.Keys.CopyTo(hello,0);
s.Values.CopyTo(hello1, 0);
for(int j=0;j<s.Count;j++)
{
this.richTextBox1.Text += hello[j]+":"+hello1[j]+"\n";
}
}
//private ManagementClass MyWMI;
//private ManagementObjectSearcher MySearcher;
}
}
程序运行后,生成结果如下:(由于在WIN7下运行,部分名称可能和其它系统不一致,但基本含义一样,可仿照修改)
PasswordHistoryLength:0 //强制密码历史 ( 对应于本地安全策略设置)
FullName:
MaxPasswordAge:3628800 //密码最长使用期限( 对应于本地安全策略设置)
UserFlags:66049
Parameters:
PasswordExpired:0 //用户下次登录时必须更改密码 (这个选项和“密码永不过期”+“用户不能更改密码” 是互斥的 可作为判断依据)
HomeDirectory:
MaxBadPasswordsAllowed:0 //账户锁定阈值 ( 对应于本地安全策略设置)
BadPasswordAttempts:0
LoginHours:System.Byte[]
objectSid:System.Byte[]
MaxStorage:-1
LastLogin:2011/11/11 19:51:23
PasswordAge:63339
LoginScript:
Name:Administrator
PrimaryGroupID:513
AutoUnlockInterval:1800 //重置账户锁定计数器 ( 对应于本地安全策略设置)
MinPasswordLength:0
Description:管理计算机(域)的内置帐户
Profile:
MinPasswordAge:0
LockoutObservationInterval:1800 //账户锁定时间 ( 对应于本地安全策略设置)