juan juan - 4 days ago 5
C# Question

How can I get the current user directory?

Using this:

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)


I get this output:

"C:\\Documents and Settings\\[USER]\\Application Data"


How can I get the root directory of all users? i.e.:

"C:\\Documents and Settings\\[USER]\\"

Answer

May be this will be a good solution: taking in account whether this is Vista/Win7 or XP and without using environment variables:

string path = Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).FullName;
if ( Environment.OSVersion.Version.Major >= 6 ) {
    path = Directory.GetParent(path).ToString();
}

Though using the environment variable is much more clear.

Comments