|
利用DirectoryEntry組件來查看網(wǎng)絡(luò),顯示域用戶的列表, 查詢客戶端域賬戶 收藏
摘要 System.DirectoryServices.DirectoryEntry組件提供了對Active Directory的訪問。本文以兩個簡單的小程序為例,闡述了如何利用此組件查看網(wǎng)絡(luò)的各節(jié)點的信息。 -------------------------------------------------------------------------------- 目錄
問題的提出 問題的初步解決 改進(jìn)后的Windows Forms方案 總結(jié) 參考文獻(xiàn) 附注 作者
-------------------------------------------------------------------------------- 問題的提出
剛接觸.Net網(wǎng)絡(luò)編程的時候,我常想,有沒有辦法列出局域網(wǎng)中的所有計算機(jī)呢?直到最近看了MSDN中關(guān)于DirectoryEntry 類的介紹,這才找到了答案。 問題的初步解決 DirectoryEntry組件提供了Path屬性,根據(jù)文檔,此屬性指定了目錄服務(wù)中用來訪問對象的對象名,其格式如下: protocol://servername:port number/distinguished name 此語句的第一部分定義了訪問將使用的協(xié)議,如 LDAP: (Lightweight Directory Access Protocol) IIS: (提供IIS元數(shù)據(jù)來讀及配置Internet Infomation Server) WinNT: (提供在非常有限的性能下對Windows NT域的訪問) NDS: (提供對Novell Directory Service的訪問) 等等(詳細(xì)信息清參考MSDN)。 據(jù)此,我們構(gòu)造了一個DirectoryEntry實例,將它的Path設(shè)為"WinNT:",以通過對它的所有子項的枚舉來發(fā)現(xiàn)網(wǎng)絡(luò)上的所有域(以及工作組)。這樣,再對所發(fā)現(xiàn)的域(以及工作組)的子項進(jìn)行枚舉,就可以發(fā)現(xiàn)網(wǎng)絡(luò)上的所有計算機(jī)。下面的一個控制臺小程序演示了這一點。 -------------------------------------------------------------------------------- using System;
using System.DirectoryServices; class TempClass
{ static void Main() { EnumComputers(); } static void EnumComputers()
{ using(DirectoryEntry root = new DirectoryEntry("WinNT:")) { foreach(DirectoryEntry domain in root.Children) { Console.WriteLine("Domain | WorkGroup:\t"+domain.Name); foreach(DirectoryEntry computer in domain.Children) { Console.WriteLine("Computer:\t"+computer.Name); } } } } } -------------------------------------------------------------------------------- 改進(jìn)后的Windows Forms方案
上面代碼中兩個嵌套的foreach循環(huán)看起來并不是太好,并且控制臺的顯示效果也并不那么美觀。下面,我將對代碼進(jìn)行一些改動,并將它移植到WinForm上。 新建一個Windows Application[C#],在Form上添加一個TreeView,命名為treeView1。 添加以下幾個函數(shù): -------------------------------------------------------------------------------- //用指定的文本構(gòu)造一個節(jié)點,將其添加為參數(shù)parant的子節(jié)點,并返回剛構(gòu)造的節(jié)點
private TreeNode AddNode(TreeNode parant,string text) { TreeNode node = new TreeNode(text); parant.Nodes.Add(node); return node; } //遞歸地找到參數(shù)entry的所有子節(jié)點,并在treeView1中顯示;這里的entry與entryNode需相對應(yīng)
private void EnumChildren(DirectoryEntry entry,TreeNode entryNode) { if(entry.Children!=null) //如果無子節(jié)點則結(jié)束 { foreach(DirectoryEntry i in entry.Children) { //將各子節(jié)點加入TreeView,并進(jìn)行遞歸 EnumChildren(i,AddNode(entryNode,i.Name)); } } } //用給定的字符串構(gòu)造根節(jié)點,并列出其所有子節(jié)點
private void Enumerate(string path) { try { using(DirectoryEntry root = new DirectoryEntry(path)) { TreeNode node = new TreeNode(root.Name); treeView1.Nodes.Add(node); EnumChildren(root,node); } } catch {} } -------------------------------------------------------------------------------- 這樣,通過傳遞 "WinNT:" 給函數(shù)Enumerate(string),就可以在TreeView中看到網(wǎng)絡(luò)上的所有計算機(jī),以及每臺計算機(jī)上的用戶、組、服務(wù)等資源
-------------------------------------------------------------------------------- 總結(jié)
本文主要介紹了用DirectoryEntry組件來瀏覽網(wǎng)絡(luò)中的各節(jié)點計算機(jī)的信息,實際上,DirectoryEntry組件功能強(qiáng)大,例如將"IIS:"作為DirectoryEntry的Path屬性,就可以列出域中運行著IIS(Internet Infomation Server)的服務(wù)器,并可獲得IIS元數(shù)據(jù)等屬性;此外,還可以用它來對網(wǎng)絡(luò)進(jìn)行遠(yuǎn)程管理與配置,有興趣者不妨一試。 asp.net獲取客戶端域賬戶
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
step 1 <script language=javascript> var wshNetwork = new ActiveXObject("WScript.Network"); alert("域名 = "+ wshNetwork.UserDomain); alert("計算機(jī)名 = "+ wshNetwork.ComputerName); alert("登錄用戶名 = "+ wshNetwork.UserName); </script> step2 1、打開IE瀏覽器,找到工具菜單 open the IE and click the "tool" on the menu bar 2、點擊IE屬性
Click the "Internet Option " 3、切換到安全頁,選擇Internet,點擊自定義級別按鈕
select the tag of "security" -> "Internet" and then click the button" Custom Level" 4、找到用戶認(rèn)證,選擇自動使用當(dāng)前用戶名密碼登錄,點擊確定按鈕,再次點擊確定關(guān)閉窗口Browse and find the "User Authentication ", select the "Automatic logon with current username and password". And Click "OK"
本文來自CSDN博客,轉(zhuǎn)載請標(biāo)明出處:http://blog.csdn.net/zhanghb0604/archive/2010/07/07/5719477.aspx
|
|
|