简体   繁体   中英

Removing a windows user using C# (Remotely)

I have been researching for a solution for some time now:

I have tried System.DirectoryServices.AccountManagement and System.DirectoryServices.

Currently i have gotten closes with System.Directory services. Here is my code:

// Connect to pc    
DirectoryEntry locaDirectoryEntry = new DirectoryEntry("WinNT://" + machineId);
// Find user by userName
    DirectoryEntry user = locaDirectoryEntry.Children.Find(userName);
// Remove the user
    locaDirectoryEntry.Children.Remove(user);
// Commit the changes
    locaDirectoryEntry.CommitChanges();

Now this code delets the user so i cannot see it within "Local Users and Groups -> Users" but the user profile however stays and turns into "Account Unknown"

Now i have been on numerous websites including this but have not been able to find something that does the trick "completely". I need the user profile to be deleted.

Any help/ thoughts is appreciated.

Okay i will answer my own question.

The code i specified above deletes a login from the computer but does not take care of the user profiles as stated by Damien_The_Unbeliever.

I have been digging in my PowerShell equivalent to the app i am making and found how i did it there. I use WMI to delete the user profile.

Here is my working code for any soul who could use it:

        public string RemoveUser(string machineId, string userName)
    {
        string result = null;

        try
        {
            // Create scope and set to computer root.
            ManagementScope scope = new ManagementScope(@"\\" + machineId + @"\root\cimv2");

            // Connect.
            scope.Connect();

            // Create the query for user profiles and a searcher.
            SelectQuery query = new SelectQuery("Win32_UserProfile");
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

            // Go through each WMI Instance
            foreach (ManagementObject mo in searcher.Get())
            {
                // Normalize username
                string normalUser = mo["LocalPath"].ToString().Split('\\').Last(); 

                // Check whether this is the user to be deleted
                if (normalUser == userName)
                {
                    mo.Delete();
                    result = "Found user: " + userName + ". Deleting...";
                }

            }

            // This code deletes a user login
            //DirectoryEntry locaDirectoryEntry = new DirectoryEntry("WinNT://" + machineId);
            //DirectoryEntry user = locaDirectoryEntry.Children.Find(userName);
            //locaDirectoryEntry.Children.Remove(user);
            //locaDirectoryEntry.CommitChanges();
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }

        return result;
    }

See link for properties on UserProfiles: https://msdn.microsoft.com/en-us/library/ee886409(v=vs.85).aspx

This is the Delete method: https://docs.microsoft.com/en-us/dotnet/api/system.management.managementobject.delete?view=netframework-4.7.2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM