2009-03-12 40 views
18

Có ai có thể cung cấp ví dụ về cách lặp qua System.DirectoryServices.PropertyCollection và xuất ra tên và giá trị thuộc tính không?Làm cách nào để lặp qua PropertyCollection

Tôi đang sử dụng C#.

@JaredPar - Thuộc tính sở hữu không có thuộc tính Tên/Giá trị. Nó có một PropertyNames và Values, gõ System.Collection.ICollection. Tôi không biết loại đối tượng basline tạo nên đối tượng PropertyCollection.

@JaredPar lần nữa - Ban đầu tôi đã gắn nhãn sai câu hỏi với loại sai. Đó là điều xấu của tôi.

Cập nhật: Dựa trên đầu vào Zhaph - Ben Duguid, tôi có thể phát triển mã sau đây.

using System.Collections; 
using System.DirectoryServices; 

public void DisplayValue(DirectoryEntry de) 
{ 
    if(de.Children != null) 
    { 
     foreach(DirectoryEntry child in de.Children) 
     { 
      PropertyCollection pc = child.Properties; 
      IDictionaryEnumerator ide = pc.GetEnumerator(); 
      ide.Reset(); 
      while(ide.MoveNext()) 
      { 
       PropertyValueCollection pvc = ide.Entry.Value as PropertyValueCollection; 

       Console.WriteLine(string.Format("Name: {0}", ide.Entry.Key.ToString())); 
       Console.WriteLine(string.Format("Value: {0}", pvc.Value));     
      } 
     }  
    } 
} 

Trả lời

5

Các PropertyCollection có một bộ sưu tập PropertyName - đó là một tập hợp các chuỗi (xem PropertyCollection.ContainsPropertyCollection.Item cả trong đó có một chuỗi). Bạn có thể gọi số GetEnumerator để cho phép bạn liệt kê bộ sưu tập, sử dụng các phương thức liệt kê thông thường - trong trường hợp này bạn sẽ nhận được một IDictionary chứa khóa chuỗi, và sau đó là một đối tượng cho mỗi mục/giá trị.

+0

Tốt hơn bạn nên sử dụng chuyển đổi Implicit trong vòng lặp foreach. –

0

EDIT tôi không nhận định OP là có PropertyValueCollection nói không PropertyCollection. Rời khỏi bài đăng vì các bài đăng khác đang tham chiếu.

Tôi không chắc mình hiểu những gì bạn đang hỏi Bạn có muốn lặp qua từng giá trị trong bộ sưu tập không? Nếu vậy mã này sẽ làm việc

PropertyValueCollection collection = GetTheCollection(); 
foreach (object value in collection) { 
    // Do something with the value 
} 

In ra Tên/Giá trị

Console.WriteLine(collection.Name); 
Console.WriteLine(collection.Value); 
25

Xem giá trị của PropertyValueCollection khi chạy trong cửa sổ đồng hồ để xác định các loại nguyên tố, nó chứa & bạn có thể mở rộng trên nó để xem thêm những gì thuộc tính của mỗi phần tử.

Thêm mã @ JaredPar của

 

PropertyCollection collection = GetTheCollection(); 
foreach (PropertyValueCollection value in collection) { 
    // Do something with the value 
    Console.WriteLine(value.PropertyName); 
    Console.WriteLine(value.Value); 
    Console.WriteLine(value.Count); 
} 
 

EDIT: PropertyCollection được tạo thành từ PropertyValueCollection

+0

Đây là cách phù hợp, có vẻ như PropertyValueCollection là chìa khóa để liệt kê đúng. Tất cả các giải pháp khác đề xuất một chỉ mục gián tiếp khác (hoặc không hoạt động). –

+0

Hoàn hảo, ngoại trừ trong trường hợp của tôi, tôi cần sử dụng '.ToString' trên' .Value' vì nó không thể được đúc hoàn toàn. – Chad

2
foreach(var k in collection.Keys) 
{ 
    string name = k; 
    string value = collection[k]; 
} 
4
usr = result.GetDirectoryEntry(); 
foreach (string strProperty in usr.Properties.PropertyNames) 
{ 
    Console.WriteLine("{0}:{1}" ,strProperty, usr.Properties[strProperty].Value); 
} 
+1

Vui lòng giải thích mã của bạn. – hims056

0

Tôi nghĩ rằng có một cách dễ dàng hơn

foreach (DictionaryEntry e in child.Properties) 
{ 
    Console.Write(e.Key); 
    Console.Write(e.Value); 
} 
+0

Tôi nhận được System.InvalidCastException: Truyền được chỉ định không hợp lệ. khi sử dụng. – Despertar

0

Bạn thực sự không cần phải làm bất cứ điều gì kỳ diệu nếu bạn muốn chỉ là một vài mặt hàng ...

Sử dụng câu lệnh: System, Hệ thống .DirectoryServices và System.AccountManagement

public void GetUserDetail(string username, string password) 
{ 
    UserDetail userDetail = new UserDetail(); 
    try 
    { 
     PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, "mydomain.com", username, password); 

     //Authenticate against Active Directory 
     if (!principalContext.ValidateCredentials(username, password)) 
     { 
      //Username or Password were incorrect or user doesn't exist 
      return userDetail; 
     } 

     //Get the details of the user passed in 
     UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext, principalContext.UserName); 

     //get the properties of the user passed in 
     DirectoryEntry directoryEntry = userPrincipal.GetUnderlyingObject() as DirectoryEntry; 

     userDetail.FirstName = directoryEntry.Properties["givenname"].Value.ToString(); 
     userDetail.LastName = directoryEntry.Properties["sn"].Value.ToString(); 
    } 
    catch (Exception ex) 
    { 
     //Catch your Excption 
    } 

    return userDetail; 
} 
0

Tôi đã đăng câu trả lời của mình trên một chủ đề khác, và sau đó tìm thấy chuỗi này hỏi một câu hỏi tương tự.

Tôi đã thử các phương pháp được đề xuất nhưng tôi luôn nhận được ngoại lệ truyền không hợp lệ khi truyền tới DictionaryEntry. Và với một DictionaryEntry, những thứ như FirstOrDefault rất sôi nổi. Vì vậy, tôi chỉ cần làm điều này:

var directoryEntry = adUser.GetUnderlyingObject() as DirectoryEntry; 
directoryEntry.RefreshCache(); 
var propNames = directoryEntry.Properties.PropertyNames.Cast<string>(); 
var props = propNames 
    .Select(x => new { Key = x, Value = directoryEntry.Properties[x].Value.ToString() }) 
    .ToList(); 

Với điều đó tại chỗ, tôi có thể dễ dàng truy vấn trực tiếp bất kỳ thuộc tính nào bằng Khóa. Sử dụng toán tử điều hướng kết hợp và an toàn cho phép đặt mặc định thành một chuỗi trống hoặc bất kỳ điều gì ..

var myProp = props.FirstOrDefault(x => x.Key == "someKey"))?.Value ?? string.Empty; 

Và nếu tôi muốn xem qua tất cả các đạo cụ, nó cũng tương tự như vậy.

foreach (var prop in props) 
{ 
    Console.WriteLine($"{prop.Key} - {prop.Value}"); 
} 

Lưu ý rằng đối tượng "adUser" là đối tượng UserPrincipal.

0
public string GetValue(string propertyName, SearchResult result) 
{ 
    foreach (var property in result.Properties) 
    { 
     if (((DictionaryEntry)property).Key.ToString() == propertyName) 
     { 
      return ((ResultPropertyValueCollection)((DictionaryEntry)property).Value)[0].ToString(); 
     } 
    } 
    return null; 
} 
Các vấn đề liên quan