有人可以提供一个示例,说明如何遍历System.DirectoryServices.PropertyCollection并输出属性名称和值吗?
我在使用C#。
@JaredPar - PropertyCollection没有Name/Value属性。它有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));
}
}
}
}