Getting user account names fails for 64bit Windows 7

1.5k views Asked by At

My C# winform application fails to get the (local machine's) user account names when installed on a 64bit Windows 7 machine. It works correctly on 32bit Windows 7, 64bit VIsta, 32 bit Vista and 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."

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)?

Note 1: The line "DirectoryEntry localMachine = new DirectoryEntry..." correctly gets the machine name.

Note 2: For simplicity, I shortened the strings by substituting in "[APLICATION NAME]." The code performs identically when using "[APLICATION NAME].ResourceAdmin.administrators" or simply "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. I tried this and it did not help.

System.DirectoryServices.DirectoryServicesPermission permission = new System.DirectoryServices.DirectoryServicesPermission(System.Security.Permissions.PermissionState.Unrestricted); permission.Assert();

1

There are 1 answers

2
dexter On

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.