我用下面的代码遍历本机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"]就可以了
浙公网安备 33010602011771号