gpt4 book ai didi

C# 目录服务双重添加到列表

转载 作者:太空宇宙 更新时间:2023-11-03 11:02:26 25 4
gpt4 key购买 nike

我有一个目录服务方法可以从 Active Directory 收集机器并将它们作为 List<> 返回.

代码是:

public static List<string> PCsAndAttributes(string PCName, string AttributeToRead)
{
List<string> toReturn = new List<string>();
try
{
string LdapPath = "LDAP://corp.company.com";
DirectoryEntry computer = new DirectoryEntry(LdapPath);

DirectorySearcher search = new DirectorySearcher(PCName);

string searchAtt = "Name";

search.Filter = "(" + searchAtt + "=" + PCName + ")";

search.PropertiesToLoad.Add(AttributeToRead);

SearchResultCollection results = search.FindAll();

foreach (SearchResult result in results)
{
ResultPropertyCollection PCS = result.Properties;

if (!(toReturn.Contains(Convert.ToString(PCS))))
{
toReturn.Add(Convert.ToString(PCS[AttributeToRead][0]));
}
}

return toReturn;
}
catch (Exception err)
{
toReturn.Add(err.Message);
return toReturn;
}
}

出于某种原因,这会在我的 TreeView 中创建每台计算机中的两台。我 99% 确定错误出在这个阶段,但我无法消除重复项。

这里是 TreeView 节点代码:

    private void UpdateLists()
{
List<string> AdFinds = ProfileCleaner.smallClasses.AdClasses.PCsAndAttributes(txtComputers.Text, "Name");
lblCount.Text = "PC Count: " + AdFinds.Count();

foreach (string PC in AdFinds)
{
string online = ProfileCleaner.smallClasses.PingIt.Pingit(PC);

if (online == "Success")
{
TreeNode pNode = treePCs.Nodes.Add(PC);
pNode.Checked = true;

string OS = ProfileCleaner.smallClasses.OsVersion.GetOsVersion.OSVersion(PC);

string SubPath = null;
if (OS == "6")
{
SubPath = @"\C$\Users\";
}
else
{
SubPath = @"\C$\Documents and Settings\";
}

try
{
string[] usrs = Directory.GetDirectories(@"\\" + PC + SubPath);
foreach (string usr in usrs)
{
List<string> noAdds = new List<string>();
noAdds.Add("admin"); noAdds.Add("Administrator");

string[] lName = usr.Split('\\');
string user = Convert.ToString(lName[lName.Length - 1]);

if (!(noAdds.Contains(user)))
{
pNode.Nodes.Add(usr);
}
}
}
catch (Exception folderErr)
{
}
}
}
}

有人能告诉我为什么我从 Active Directory 获得每台机器中的两台吗?

我曾尝试捕捉和消除,也许这是我的逻辑,但我尝试过这样的事情:

if (!(myList.contains(NewMachine)) { } 

并没有阻止他们。

最佳答案

在事件目录中,计算机既是用户又是计算机。您的搜索字符串应该是 search.Filter = "(&("+ searchAtt + "="+ PCName + ")(objectclass=computer))";

关于C# 目录服务双重添加到列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17200832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com