程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> C#裡根據Windows本地用戶組查詢組成員,以及修改密碼

C#裡根據Windows本地用戶組查詢組成員,以及修改密碼

編輯:C#基礎知識

使用(System.DirectoryServices.Protocols使用"WinNT://"方式)

當前使用這個方式查找組成員的方法資料實在是太少.添加用戶\組等的功能,倒是能search到好多.用訪問LDAP方式可以很輕松的用DirectorySearcher來查詢出結果,但是部分機器沒裝加入域裡,就不能行的通了.代碼貼出來分享一下,惡意者看完請不要評價,直接閃人.謝謝!

主要的類就是這個,DEMO也有一個,要的聯系我吧[email protected].代碼裡有注釋,就不介紹代碼了,類方法寫的有小BUG,一般情況下不會出錯,但是當你的機器用戶組裡存在用戶,並且你無論在注冊表裡,或者是命令行下等都發現不了的時候,就會出錯,這裡我沒有寫上解決方案.(我在寫C#代碼創建本地用戶且設置主文件夾權限的時候,搞出來幾個名字N長的用戶,不知道怎麼回事).如果誰有更簡單的方法請分享,謝謝!

using System;
using System.DirectoryServices;  //這兩個using一定要寫上去
using System.DirectoryServices.Protocols;
using System.Collections;//要在reference裡添加System.DirectoryServices.dll和System.DirectoryServices.Protocols.dll

namespace LocalGroupUserTest
{
    /// <summary>
    /// 主要包括下面兩個方法,其他方法也可以用,主要為輔助主方法用
    /// UpdatePassWord(string var)  修改本地密碼的方法
    /// GetUserInfoArrayList(string var)    根據用戶組,查詢本地包含用戶HashTable(含名稱、全名、描述)的數組
    /// </summary>
    public class GroupUserOP
    {
        /// <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("test", "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" }));
                   


                    ht.Add("Username", userName);
                    ht.Add("FullName", FullName);
                    ht.Add("Description", Description);                  
                }
                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;
        }
    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved