2009-03-12 30 views
18

System.DirectoryServices.PropertyCollectionをループしてプロパティ名と値を出力する方法の例は誰でも提供できますか?PropertyCollectionをループする方法

私はC#を使用しています。

@ JaredPar - PropertyCollectionには名前/値プロパティはありません。 PropertyNamesとValuesがあり、System.Collection.ICollectionと入力します。私は、PropertyCollectionオブジェクトを構成するベースラインオブジェクトタイプを知らない。

@JaredPar再び - 私はもともと誤ったタイプの質問に間違ったラベルを付けました。それは私の悪かった。

更新: Zhaph-Ben Duguidの入力に基づいて、次のコードを作成することができました。

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));     
      } 
     }  
    } 
} 

答えて

5

で構成されてPropertyCollectionがPropertyName意味のコレクションがある - 文字列のコレクションがある(PropertyCollection.ContainsPropertyCollection.Itemの両方を参照してくださいそのうち文字列を取ります)。

普通の列挙方法を使用してコレクションを列挙できるようにするには、通常GetEnumeratorを呼び出すことができます。この場合、文字列キーを含むIDictionaryを取得し、各項目/値のオブジェクトを取得します。システム、システム:文を使用して、私は

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

foreachループで暗黙の変換を使用する方が良いでしょう。 –

0

EDITは、私が言ったPropertyValueCollectionないPropertyCollectionを持つようOPを読み違えます。他の投稿がそれを参照しているため投稿を終了します。

私はあなたが尋ねていることを理解していませんコレクションの各値をループしたいだけですか?ので、このコードは、要素の種類を識別するためのウォッチウィンドウで、実行時にPropertyValueCollectionの値を参照してください名前/値

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

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

印刷をうまくいく場合は、上の展開することができ&、それが含まれています要素のそれぞれがどのような性質を持っているかをさらに見ることができます。 @ JaredParのコードに追加

 

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がPropertyValueCollection

+0

これは正しい方法ですが、PropertyValueCollectionが正しい列挙のキーであるようです。他のすべてのソリューションは、別の間接的なインデックス作成を提案します(またはどちらかが機能しない)。 –

+0

私の場合を除いて、 '.ToString'は暗黙的にキャストできないため、' .Value'に使う必要がありました。 – 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

コードを説明してください。 – hims056

0

...

.DirectoryServices、および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

System.InvalidCastExceptionがあります:指定されたキャストは無効です。これを使用するとき。 – Despertar

0

は、あなたが本当にあなただけのいくつかの項目をしたい場合は魔法何もする必要はありませんもっと簡単な方法があると思う

0

私は別のスレッドで自分の答えを投稿し、このスレッドは同様の質問をします。

私は提案された方法を試しましたが、DictionaryEntryにキャストするときに常に無効なキャスト例外が発生します。 DictionaryEntryでは、FirstOrDefaultのようなものはファンキーです。だから、私はこれを行うだけです:

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(); 

これで、私は簡単にKeyで直接任意のプロパティを照会できます。合体で安全なナビゲーション演算子を使用すると...空の文字列または何をデフォルトのため

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

を可能にし、私はすべての小道具を超える見たいと思った場合、それは同様のforeachのです。

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

「adUser」オブジェクトが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; 
} 
関連する問題