[英]Getting user account names fails for 64bit Windows 7
My C# winform application fails to get the (local machine's) user account names when installed on a 64bit Windows 7 machine. 当安装在64位Windows 7计算机上时,我的C#winform应用程序无法获取(本地计算机的)用户帐户名。 It works correctly on 32bit Windows 7, 64bit VIsta, 32 bit Vista and XP. 它可以在32位Windows 7、64位VIsta,32位Vista和XP上正常运行。
The code fails on the line "DirectoryEntry admGroup = localMachine.Children.Find..." with the error "System.Runtime.InteropServices.COMException [0x800708ac]. The group name could not be found." 代码在“ DirectoryEntry admGroup = localMachine.Children.Find ...”行上失败,错误为“ System.Runtime.InteropServices.COMException [0x800708ac]。找不到组名。”
What change can I make to the code to get it to work for 64bit Windows 7 (that also works for all the other operating systems)? 我可以对代码进行什么更改,以使其在64位Windows 7(也适用于所有其他操作系统)上运行?
Note 1: The line "DirectoryEntry localMachine = new DirectoryEntry..." correctly gets the machine name. 注意1:“ DirectoryEntry localMachine = new DirectoryEntry ...”行可以正确获取计算机名称。
Note 2: For simplicity, I shortened the strings by substituting in "[APLICATION NAME]." 注意2:为简单起见,我通过替换“ [APLICATION NAME]”来缩短字符串。 The code performs identically when using "[APLICATION NAME].ResourceAdmin.administrators" or simply "administrators." 使用“ [APLICATION NAME] .ResourceAdmin.administrators”或仅使用“ administrators”时,该代码的性能相同。
#region Get Windows User Accounts
private void GetWindowsUser()
{
DataSet dsWindowsUser = null;
try
{
//Retrieve machine name.
DirectoryEntry localMachine = new DirectoryEntry([APLICATION NAME].ResourceAdmin.WiinNT + Environment.MachineName);
//CODE FAILS ON THE NEXT LINE
DirectoryEntry admGroup = localMachine.Children.Find([APLICATION NAME].ResourceAdmin.administrators, [APLICATION NAME].ResourceAdmin.group);
// DirectoryEntry admGroup = localMachine.Children.Find("administrators", "group"); //TEST CODE
object adminmembers = admGroup.Invoke([APLICATION NAME].ResourceAdmin.members, null);
// object adminmembers = admGroup.Invoke("members", null); //TEST CODE
DirectoryEntry userGroup = localMachine.Children.Find([APLICATION NAME].ResourceAdmin.Users, [APLICATION NAME].ResourceAdmin.group);
object usermembers = userGroup.Invoke([APLICATION NAME].ResourceAdmin.members, null);
//Create datatable to store windows user.
DataTable dtWindowsUser = new DataTable();
DataRow drow;
//Create datatable to add user
DataColumn myDataColumn;
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "WindowsUser";
//Add column to datatable
dtWindowsUser.Columns.Add(myDataColumn);
//Retrieve each user name.
foreach (object groupMember in (IEnumerable)adminmembers)
{
DirectoryEntry member = new DirectoryEntry(groupMember);
if (!(member.Name == "admin" || member.Name == "Domain Admins"))
{
drow = dtWindowsUser.NewRow();
drow["WindowsUser"] = member.Name;
//Add row to datatable
dtWindowsUser.Rows.Add(drow);
}
}
foreach (object groupMember in (IEnumerable)usermembers)
{
DirectoryEntry member = new DirectoryEntry(groupMember);
if (!(member.Name == "ACTUser" || member.Name == "ASPNET" || member.Name == "Domain Users" || member.Name == "Authenticated Users" || member.Name == "INTERACTIVE" || member.Name == "SQLDebugger"))
{
drow = dtWindowsUser.NewRow();
drow["WindowsUser"] = member.Name;
//Add row to datatable
dtWindowsUser.Rows.Add(drow);
}
}
dsWindowsUser = new DataSet();
dsWindowsUser.Tables.Add(dtWindowsUser);
//Add User to database
objAdminDAO.AddUpdateUserInfo(dsWindowsUser);
}
catch (Exception ex)
{
BusinessObject.Logger.Logger.Log(ex);
}
finally
{
if (!(dsWindowsUser == null))
{
dsWindowsUser.Dispose();
}
}
}
Edit : For a similar question on another blog site it was suggested to add this code right before the "DirectoryEntry" statement that fails. 编辑 :对于另一个博客网站上的类似问题,建议在失败的“ DirectoryEntry”语句之前添加此代码。 I tried this and it did not help. 我尝试过,但没有帮助。
System.DirectoryServices.DirectoryServicesPermission permission = new System.DirectoryServices.DirectoryServicesPermission(System.Security.Permissions.PermissionState.Unrestricted); System.DirectoryServices.DirectoryServicesPermission权限=新的System.DirectoryServices.DirectoryServicesPermission(System.Security.Permissions.PermissionState.Unrestricted); permission.Assert(); 权限.Assert();
How about this: 这个怎么样:
using(PrincipalContext ctx = new PrincipalContext(ContextType.Machine)) {
UserPrincipal userPrincipal = new UserPrincipal(ctx, "myNewAccount", "myPass", true);
} }
Then take a look at methods and members of the 2 classes to learn how to do stuff with them. 然后看一下这两个类的方法和成员,以学习如何使用它们。 It's much easier to use these than the DirectoryEntry class - no LDAP strings. 比DirectoryEntry类更容易使用它们-没有LDAP字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.