【问题标题】:Getting user account names fails for 64bit Windows 7获取 64 位 Windows 7 的用户帐户名称失败
【发布时间】:2011-02-08 13:04:44
【问题描述】:

当安装在 64 位 Windows 7 计算机上时,我的 C# winform 应用程序无法获取(本地计算机的)用户帐户名称。它可以在 32 位 Windows 7、64 位 VIsta、32 位 Vista 和 XP 上正常运行。

代码在“DirectoryEntry admGroup = localMachine.Children.Find...”行失败,并出现错误“System.Runtime.InteropServices.COMException [0x800708ac]。找不到组名。”

我可以对代码进行哪些更改以使其适用于 64 位 Windows 7(也适用于所有其他操作系统)?

注意 1:“DirectoryEntry localMachine = new DirectoryEntry...”这一行正确获取了机器名称。

注意 2:为简单起见,我通过替换为“[应用程序名称]”来缩短字符串。使用“[应用程序名称].ResourceAdmin.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();
                }
            }
        }

编辑:对于另一个博客站点上的类似问题,建议在失败的“DirectoryEntry”语句之前添加此代码。我试过了,但没有帮助。

System.DirectoryServices.DirectoryServicesPermission 许可=新 System.DirectoryServices.DirectoryServicesPermission(System.Security.Permissions.PermissionState.Unrestricted); 权限.Assert();

【问题讨论】:

    标签: c# winforms directoryentry


    【解决方案1】:

    这个怎么样:

    using(PrincipalContext ctx = new PrincipalContext(ContextType.Machine)) {
    
          UserPrincipal userPrincipal = new UserPrincipal(ctx, "myNewAccount", "myPass", true);
    

    }

    然后看看这 2 个类的方法和成员,以了解如何使用它们。使用这些比 DirectoryEntry 类容易得多 - 没有 LDAP 字符串。

    【讨论】:

    • 是的,这也是我的建议,摆脱 ldap 代码并用 DirectoryServices 替换它,使用起来更容易
    • @chad:我很难将您的建议整合到我的代码中。我如何处理“myNewAccount”和“myPass”?我要保留这些字符串吗?
    猜你喜欢
    • 2017-05-12
    • 2012-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-06
    • 1970-01-01
    相关资源
    最近更新 更多