42

1)how can i find out the Windows Installation drive in which the user is working.? I need this to navigate to the ApplicationData in DocumentsandSettings.

2)Also how can i get the user name too so that i can goto ApplicaitionData.? Eg: "D:\Documents and Settings\user\Application Data".

vela
  • 147
  • 10
SyncMaster
  • 9,754
  • 34
  • 94
  • 137

6 Answers6

76

Look at combining Environment.GetFolderPath and Environment.SpecialFolder to do this.

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Martin Harris
  • 28,277
  • 7
  • 90
  • 101
22

Depending on what you are doing you might also want to look at

Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)

If the user is on a domain it will only be stored in their local AppData folder and not synced with their roaming profile.

TotPeRo
  • 6,561
  • 4
  • 47
  • 60
Twelve47
  • 3,924
  • 3
  • 22
  • 29
  • Thank you, exactly the situation I needed to deal with. The stuff I need is indeed in AppData/Local and not AppData/Roaming, which is returned by default by this call – PandaWood Jun 28 '13 at 02:19
5

Have a look at the Environment.SpecialFolders

Environment.SpecialFolder.ApplicationData;
Environment.SpecialFolder.System

that should get you round the username requirement as well.

Eoin Campbell
  • 43,500
  • 17
  • 101
  • 157
3

Have a look at the System.Environment class and its properties and methods, e.g:

string systemDir = System.Environment.SystemDirectory;
string docs = System.Environment.GetFolderPath(
    System.Environment.SpecialFolder.MyDocuments));

string systemDrive = System.IO.Path.GetPathRoot(systemDir);

The first one returns "C:\Windows\system32" for example and the second one "C:\Documents and Settings\USERNAME\My Documents".

M4N
  • 94,805
  • 45
  • 217
  • 260
2

Try this:

string filePath = Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);
Magnus Johansson
  • 28,010
  • 19
  • 106
  • 164
1

1)how can i find out the Windows Installation drive in which the user is working.?

    var systemDrive =  Environment.ExpandEnvironmentVariables("%systemdrive%");

I need this to navigate to the ApplicationData in DocumentsandSettings.

You don't really require to fetch the value of either system drive or currently logged in user name to achieve this. There are predefined environment variables %localAppData% and %appData% which give you fully qualified path of these directories as shown in the code below:

var localApplicationData = Environment.ExpandEnvironmentVariables("%localappdata%"); 
//this gives C:\Users\<userName>\AppData\Local

var roamingApplicationData = Environment.ExpandEnvironmentVariables("%appdata%");
//this gives C:\Users\<userName>\AppData\Roaming

2)Also how can i get the user name too so that i can goto ApplicaitionData.? Eg: "D:\Documents and Settings\user\Application Data".

Again, you don't need user name to get the application data path as I've discussed above. Still, for the sake of knowledge you can fetch it from %username% environment variable as shown below:

    var currentUserName = Environment.ExpandEnvironmentVariables("%username%");
RBT
  • 24,161
  • 21
  • 159
  • 240