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,738
  • 21
  • 105
  • 131
Hema Joshi
  • 247
  • 1
  • 5
  • 10

4 Answers4

12

Environment.SpecialFolder.ApplicationData and Environment.SpecialFolder.CommonApplicationData

Sergej Andrejev
  • 9,091
  • 11
  • 71
  • 108
11

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

string path = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
hultqvist
  • 17,451
  • 15
  • 64
  • 101
  • 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
  • 326
  • 3
  • 7
  • This will only find users that are currently logged in at the time you run this code. If you want to find app data for all of the users that ever logged into a given machine you will have to first determine all of the users that have logged into the given machine and then individually load the individual registry hives of each user to obtain the path name of each user's appdata directory. Of course you have to have elevated privileges to load user registry hives. – CodeWhore Nov 14 '22 at 23:35
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,502
  • 2
  • 24
  • 34