If you are a developer and using C# .NET to build your application, you may need to access the Application Data (AppData) folder of all the user accounts currently created in a specific system. The .NET framework exposes API to detect only the logged-in user's AppData folder.

 

To get the path for all the users of the system, you have to find it from the Windows Registry. Today we will discuss on that topic with a piece of code. Continue reading.

 

How to retrieve AppData folder path for all user accounts? (www.kunal-chowdhury.com)

 

To get the local application data (AppData) folder of the current logged-in user, you can call the API Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); which will return the absolute folder path. But to grab the folder path of all the users of the system, we have to find it out from the Windows Registry.

 

Basically it resides in the "HKEY_USERS" path, separated in specific section under SID key of the users. If you open the registry editor and navigate to "Computer\HKEY_USERS", you will find a list of SIDs which are nothing but the unique IDs for each user accounts on that system.

 

Registry Path to retrieve users SID (www.kunal-chowdhury.com)

 

You may want to read:  Programmatically retrieve Visual Studio install directory

 

 

Expand any one of the SID and navigate to the following path: Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders. If in that path you find the key named "Local AppData", the string value of it returns the absolute path of the local application data folder of that user specified by SID. Here is the screenshot of the same from Windows Registry, for your reference:

 

Registry Path to retrieve users AppData directory (www.kunal-chowdhury.com)

 

Now is the time to retrieve it from the registry using C# code. First you need to get all the SIDs under HKEY_USERS and then iterate through them to find the path. Below is the code snippet which you can use to grab the same:

 

/// <summary>
/// Gets the AppData folder path for all users of the system.
/// </summary>
/// <returns>List of AppData folder path</returns>
/// <exception cref="System.Security.SecurityException"/>
/// <exception cref="System.UnauthorizedAccessException"/>
private static List GetAppDataFolderPathForAllUsersOfTheSystem()
{
    const string regValueLocalAppData = @"Local AppData";
    const string regKeyShellFolders = @"HKEY_USERS\$SID$\Software\Microsoft\Windows\" +
                                      @"CurrentVersion\Explorer\Shell Folders";
 
    var sidKeys = Registry.Users.GetSubKeyNames();
    var appDataPathsForAllUsers = new List();
 
    foreach (var sidKey in sidKeys)
    {
        var localAppDataPath = Registry.GetValue(regKeyShellFolders.Replace("$SID$", sidKey),
                                                 regValueLocalAppData, null) as string;
        if (!string.IsNullOrWhiteSpace(localAppDataPath))
        {
            appDataPathsForAllUsers.Add(localAppDataPath);
        }
    }
 
    return appDataPathsForAllUsers;
}
  

 

I hope that the post was useful and will help you to dynamically find all the users AppData folder available in that system. Make sure to run this code under admin privileges, as it needs admin rights to access the Windows Registry. Otherwise it will throw System.Security.SecurityException or System.UnauthorizedAccessException as mentioned in the method level comments above.

 

 

Have a question? Or, a comment? Let's Discuss it below...

dhgate

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.