[英]Check if a DirectoryEntry is the last of a Group in Active Directory C#
Hello, I need to check if a DirectoryEntry
is the last one of a Group in AD . 您好,我需要检查
DirectoryEntry
是否是AD中Group的最后一个 。 I don't know why, but my boss says he want a Messagebox. 我不知道为什么,但是我的老板说他想要一个Messagebox。 The method below is just a little part of my big project.
下面的方法只是我的大型项目的一小部分。 What it does?
它能做什么? I gets all users of a group in AD.
我得到了广告组中的所有用户。 Every user is saved in a
DirectoryEntry
. 每个用户都保存在
DirectoryEntry
。 Then I am calling some other stuff. 然后我打电话给其他东西。 This is irrelevant for this Question see "Do Stuff if de".
这与本问题无关,请参见“如果做则做事”。
I don't know how to get the last element thats why I put this: 我不知道如何获得最后一个元素,这就是为什么我这样说:
var lastelement = users.LastElement;
in the method. 在方法中。
private void Abgleich()
{
log.Debug("Abgleich in ActivDirectory aufgerufen");
using (var context = new PrincipalContext(ContextType.Domain, Properties.Settings.Default.Servername, Properties.Settings.Default.Container))
{
using (var group = GroupPrincipal.FindByIdentity(context, Properties.Settings.Default.ECADGruppe))
{
if (group == null)
{
log.Error("Group does not exist");
}
else
{
var users = group.GetMembers(true);
//Pseudo Code
var lastelement = users.LastElement;
//End Pseudo Code
foreach (UserPrincipal user in users)
{
DirectoryEntry de = (user.GetUnderlyingObject() as DirectoryEntry);
// Do Stuff if de
if (de == lastelement)
{
XtraMessageBox.Show("This is the last", "Remember", MessageBoxButtons.YesNo, MessageBoxIcon.Info);
}
}
}
}
}
}
There are several ways to find the last object in a collection. 有几种方法可以找到集合中的最后一个对象。
Note: Intellisense won't show all methods if you use var instead of classname in declarations.
注意:如果在声明中使用var而不是classname,则Intellisense不会显示所有方法。
One way is to store the last object of the collection in a variable 一种方法是将集合的最后一个对象存储在变量中
PrincipalSearchResult<Principal> users = group.GetMembers(true);
UserPrincipal lastuser = (UserPrincipal)users.Last();
foreach (UserPrincipal user in users)
{
//...
if (user == lastuser)
{
// Messagebox
}
}
Other option is to use a iterate variable 另一种选择是使用迭代变量
for (int i = 0; i < users.Count(); i++)
{
if (i == users.Count())
{
// .. last user
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.