Henry
Henry

Reputation: 95

How can the folder of a user account under c:\users be delete when delete windows account?

I am trying to programmatically delete windows accounts along with their corresponding folders under c:\Users in windows 2008R2.
I call net user [user name] /delete command in the program but it does not delete the folder "user name" under c:\users. The DelectFile function does not work: error code is 5.

Is there a way to solve it?

Upvotes: 0

Views: 682

Answers (2)

Harry Johnston
Harry Johnston

Reputation: 36318

You can't just DeleteFile a directory. You have to explicitly iterate through the contents of the directory, deleting each file and removing each subdirectory.

Also, be warned that the name of the user profile folder is not always identical to the username. The mapping is stored in the registry somewhere.

Upvotes: 1

akton
akton

Reputation: 14386

Error code 5 means "access denied". To fix it, check that the local system account has permission to delete that folder. You can do it from the command line using "cacls". LocalSystem should have access by default, as a member of the Administrators group, but that may have been removed. If it is missing, take ownership of the folder first.

Another problem might be that the folder is "in use", that is a handle is open with deny delete set for sharing. This is harder to solve. You may need to log the user off first or wait then retry.

Upvotes: 1

Related Questions