利用ADSI訪問本地賬戶信息

 

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  //賬戶鎖定時間 ( 對應於本地安全策略設置)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章