tags:

views:

109

answers:

1

Hi,

I have application which creates some windows user accounts, on uninstallation I remove the Windows User Account, but the folder for that user remains there (For example C:\Documents and Settings\UserName\"

How can I remove that folder using C#?

Thanks,

+2  A: 

Something along the lines of this?

DirectoryInfo dir = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
dir = dir.Parent.Parent.Parent;
DirectoryInfo[] userDirs = dir.GetDirectories(userName);

foreach (DirectoryInfo userDir in userDirs)
{
    userDir.Delete(true);
}
MPritch
That only works for the currently logged in user. The actual folder created for another account is far more difficult to come by.
Paul Alexander
Agree, but they all reside in the same directory (e.g. C:\Documents and Settings\), don't they? The Parent.Parent.Parent bit moves up to that common directory, then the GetDirectories(userName) bit moves to the relevant subdirectory. Agree though, this could be improved!
MPritch
It works with small change:dir = dir.Parent.Parent.Parent;dir = dir.Parent.Parent;We need to go up only 2 folders :-) Thanks
This will only work if you are using an admin account on the machine. An ordinary user account should not have access to the folder for other accounts (on a properly secured machine).
Sam Meldrum
I think that can be assumed given the nature of what is trying to be achieved :-)
MPritch