11

How do I find the application data folder path for all Windows users from C#?

How do I find this for the current user and other Windows users?

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Hema Joshi
  • 247
  • 1
  • 4
  • 10

4 Answers4

12

Environment.SpecialFolder.ApplicationData and Environment.SpecialFolder.CommonApplicationData

Sergej Andrejev
  • 8,821
  • 11
  • 68
  • 108
11

This will give you the path to the "All users" application data folder.

string path = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
hultqvist
  • 16,225
  • 15
  • 62
  • 95
  • 6
    This path only provides read access if you need read/write access check this link:http://www.codeproject.com/Tips/61987/Allow-write-modify-access-to-CommonApplicationData – mas_oz2k1 May 12 '12 at 23:48
  • This is not what asked for. This path requires elevated rights to write. – AFgone Jul 13 '19 at 20:32
7

Adapted from @Derrick's answer. The following code will find the path to Local AppData for each user on the computer and put the paths in a List of strings.

        const string regKeyFolders = @"HKEY_USERS\<SID>\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders";
        const string regValueAppData = @"Local AppData";
        string[] keys = Registry.Users.GetSubKeyNames();
        List<String> paths = new List<String>();

        foreach (string sid in keys)
        {
            string appDataPath = Registry.GetValue(regKeyFolders.Replace("<SID>", sid), regValueAppData, null) as string;
            if (appDataPath != null)
            {
                paths.Add(appDataPath);
            }
        }
bubblesdawn
  • 306
  • 3
  • 6
1

The AppData folder for each user is stored in the registry.

Using this path:

const string regKeyFolders = @"HKEY_USERS\<SID>\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders";
const string regValueAppData = @"AppData";

Given a variable sid string containing the users sid, you can get their AppData path like this:

string path=Registry.GetValue(regKeyFolders.Replace("<SID>", sid), regValueAppData, null) as string;
Derrick
  • 2,422
  • 2
  • 23
  • 32