64位Windows 7的用户帐户名称失败

时间:2011-02-08 13:04:44

标签: c# winforms directoryentry

我的C#winform应用程序在64位Windows 7计算机上安装时无法获取(本地计算机)用户帐户名。它可以在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:为简单起见,我通过替换“[APLICATION NAME]”来缩短字符串。使用“[APLICATION NAME] .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   permission = new   System.DirectoryServices.DirectoryServicesPermission(System.Security.Permissions.PermissionState.Unrestricted);   permission.Assert();

1 个答案:

答案 0 :(得分:0)

这个怎么样:

using(PrincipalContext ctx = new PrincipalContext(ContextType.Machine)) {

      UserPrincipal userPrincipal = new UserPrincipal(ctx, "myNewAccount", "myPass", true);

}

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