我用下面的代码遍历本机Users组下的所有用户,(Users用户组下有很多用户):
结果发现循环foreach里面的部分没有被执行,说明没有找到用户。
我又改用下面的代码:
运行结果如下:
可以看到,在Users组的所有的属性中没有找到一个属性可以标示它下面的所有用户
我用下面的代码遍历计算机下的所有用户:
执行结果如下:
说明遍历本地计算机遍历到了用户,为什么在组中用这种方法就遍历不到那?
下面我试图通过在用户的属性中找到一些关于所属组的信息,我用下面的代码:
运行后得到其中一个用户的属性列表如下:
可以看到,在这个用户的所有属性中,没有与组有关的信息。
那么到底该怎么样才能获得一个组下面的所有用户那?
自己搞定:
其中m_group是代表当前组的DirectoryEntry
WinUser和WinUserCollection是自定义的类和集合类
类似的,如果想获得一个用户所属的所有组,就用一个代表user的DirectoryEntry来进行Invoke["Groups"]就可以了
string group_name = "Users";
DirectoryEntry m_group = null;
DirectoryEntry AD;
AD = null;
AD = new DirectoryEntry("WinNT://" +
Environment.MachineName + ",computer");
m_group = AD.Children.Find( group_name , "group");
if( m_group == null )
{
throw new Exception("没有找到用户组" + group_name );
}
foreach( DirectoryEntry entry in m_group.Children )
{
this.Page.Response.Write( entry.Name + "<br>" );
}
DirectoryEntry m_group = null;
DirectoryEntry AD;
AD = null;
AD = new DirectoryEntry("WinNT://" +
Environment.MachineName + ",computer");
m_group = AD.Children.Find( group_name , "group");
if( m_group == null )
{
throw new Exception("没有找到用户组" + group_name );
}
foreach( DirectoryEntry entry in m_group.Children )
{
this.Page.Response.Write( entry.Name + "<br>" );
}
结果发现循环foreach里面的部分没有被执行,说明没有找到用户。
我又改用下面的代码:
string group_name = "Users";
DirectoryEntry m_group = null;
DirectoryEntry AD;
AD = null;
AD = new DirectoryEntry("WinNT://" +
Environment.MachineName + ",computer");
m_group = AD.Children.Find( group_name , "group");
if( m_group == null )
{
throw new Exception("没有找到用户组" + group_name );
}
foreach( string s in m_group.Properties.PropertyNames )
{
this.Page.Response.Write( s + ":" + m_group.Properties[s].Value.ToString() + "<br>" );
}
DirectoryEntry m_group = null;
DirectoryEntry AD;
AD = null;
AD = new DirectoryEntry("WinNT://" +
Environment.MachineName + ",computer");
m_group = AD.Children.Find( group_name , "group");
if( m_group == null )
{
throw new Exception("没有找到用户组" + group_name );
}
foreach( string s in m_group.Properties.PropertyNames )
{
this.Page.Response.Write( s + ":" + m_group.Properties[s].Value.ToString() + "<br>" );
}
运行结果如下:
groupType:4
Name:Users
Description:用户无法进行有意或无意的改动。因此,用户可以运行经过验证的应用程序,但不可以运行大多数旧版应用程序
objectSid:System.Byte[]
Name:Users
Description:用户无法进行有意或无意的改动。因此,用户可以运行经过验证的应用程序,但不可以运行大多数旧版应用程序
objectSid:System.Byte[]
可以看到,在Users组的所有的属性中没有找到一个属性可以标示它下面的所有用户
我用下面的代码遍历计算机下的所有用户:
string group_name = "Users";
DirectoryEntry m_group = null;
DirectoryEntry AD;
AD = null;
AD = new DirectoryEntry("WinNT://" +
Environment.MachineName + ",computer");
m_group = AD.Children.Find( group_name , "group");
foreach( DirectoryEntry entry in AD.Children )
{
if( entry.SchemaClassName.ToUpper() == "user".ToUpper() ) //判断是否用户
{
this.Page.Response.Write( entry.Name + "<br>" );
}
}
DirectoryEntry m_group = null;
DirectoryEntry AD;
AD = null;
AD = new DirectoryEntry("WinNT://" +
Environment.MachineName + ",computer");
m_group = AD.Children.Find( group_name , "group");
foreach( DirectoryEntry entry in AD.Children )
{
if( entry.SchemaClassName.ToUpper() == "user".ToUpper() ) //判断是否用户
{
this.Page.Response.Write( entry.Name + "<br>" );
}
}
执行结果如下:
ACTUser
Administrator
ASPNET
Guest
IUSR_FANGHUI
IWAM_FANGHUI
SQLDebugger
SUPPORT_388945a0
周星驰
Administrator
ASPNET
Guest
IUSR_FANGHUI
IWAM_FANGHUI
SQLDebugger
SUPPORT_388945a0
周星驰
说明遍历本地计算机遍历到了用户,为什么在组中用这种方法就遍历不到那?
下面我试图通过在用户的属性中找到一些关于所属组的信息,我用下面的代码:
string group_name = "Users";
DirectoryEntry AD;
AD = null;
AD = new DirectoryEntry("WinNT://" +
Environment.MachineName + ",computer");
foreach( DirectoryEntry entry in AD.Children )
{
if( entry.SchemaClassName.ToUpper() == "user".ToUpper() ) //判断是否用户
{
foreach( string propertyName in entry.Properties.PropertyNames )
{
this.Page.Response.Write( propertyName + ":" + entry.Properties[propertyName].Value.ToString() + "<br>" );
}
}
Response.Write("-------------------------------------------------------------------------------<br>");
}
DirectoryEntry AD;
AD = null;
AD = new DirectoryEntry("WinNT://" +
Environment.MachineName + ",computer");
foreach( DirectoryEntry entry in AD.Children )
{
if( entry.SchemaClassName.ToUpper() == "user".ToUpper() ) //判断是否用户
{
foreach( string propertyName in entry.Properties.PropertyNames )
{
this.Page.Response.Write( propertyName + ":" + entry.Properties[propertyName].Value.ToString() + "<br>" );
}
}
Response.Write("-------------------------------------------------------------------------------<br>");
}
运行后得到其中一个用户的属性列表如下:
UserFlags:513
MaxStorage:-1
PasswordAge:17925
PasswordExpired:0
LoginHours:System.Byte[]
FullName:周星驰
Description:方法论
BadPasswordAttempts:0
HomeDirectory:
LoginScript:
Profile:
HomeDirDrive:
Parameters:
PrimaryGroupID:513
Name:周星驰
MinPasswordLength:0
MaxPasswordAge:3710851
MinPasswordAge:0
PasswordHistoryLength:1
AutoUnlockInterval:1800
LockoutObservationInterval:1800
MaxBadPasswordsAllowed:0
objectSid:System.Byte[]
MaxStorage:-1
PasswordAge:17925
PasswordExpired:0
LoginHours:System.Byte[]
FullName:周星驰
Description:方法论
BadPasswordAttempts:0
HomeDirectory:
LoginScript:
Profile:
HomeDirDrive:
Parameters:
PrimaryGroupID:513
Name:周星驰
MinPasswordLength:0
MaxPasswordAge:3710851
MinPasswordAge:0
PasswordHistoryLength:1
AutoUnlockInterval:1800
LockoutObservationInterval:1800
MaxBadPasswordsAllowed:0
objectSid:System.Byte[]
可以看到,在这个用户的所有属性中,没有与组有关的信息。
那么到底该怎么样才能获得一个组下面的所有用户那?

自己搞定:
object obUsers = m_group.Invoke("Members");
foreach (object ob in (IEnumerable)obUsers)
{
// Create object for each user.
DirectoryEntry obUserEntry = new DirectoryEntry(ob);
winuser = new WinUser( obUserEntry.Name );
winuser.Description = obUserEntry.Properties["Description"].Value.ToString();
winusercollection.Add( winuser );
}
foreach (object ob in (IEnumerable)obUsers)
{
// Create object for each user.
DirectoryEntry obUserEntry = new DirectoryEntry(ob);
winuser = new WinUser( obUserEntry.Name );
winuser.Description = obUserEntry.Properties["Description"].Value.ToString();
winusercollection.Add( winuser );
}
其中m_group是代表当前组的DirectoryEntry
WinUser和WinUserCollection是自定义的类和集合类
类似的,如果想获得一个用户所属的所有组,就用一个代表user的DirectoryEntry来进行Invoke["Groups"]就可以了
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架