Mic*_*ern 20 c# asp.net iis directoryservices
任何人都可以提供如何循环通过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));
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
sha*_*esh 27
在监视窗口中查看运行时PropertyValueCollection的值以标识元素的类型,它包含&您可以展开它以进一步查看每个元素具有的属性.
添加到@ 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);
}
编辑:PropertyCollection由PropertyValueCollection组成
PropertyCollection有一个PropertyName集合 - 它是一个字符串集合(参见PropertyCollection.Contains和PropertyCollection.Item,它们都带有一个字符串).
您通常可以调用GetEnumerator来允许您使用通常的枚举方法枚举集合 - 在这种情况下,您将获得包含字符串键的IDictionary,然后是每个项/值的对象.
小智 5
usr = result.GetDirectoryEntry();
foreach (string strProperty in usr.Properties.PropertyNames)
{
Console.WriteLine("{0}:{1}" ,strProperty, usr.Properties[strProperty].Value);
}
Run Code Online (Sandbox Code Playgroud)