UserScopedSetting Data Store Location

R Evans 211 Reputation points
2020-03-14T19:48:15.583+00:00

I have a WPF .NET Framework 4.7.2 app that uses the UserScopedSetting attribute class to save custom settings to the users AppData\Local folder. On my development machine, the path to my user.config file looks like the following: "C:\Users\Me\AppData\Local\MyCompanyName\MyAppName.exe_Url_rtcpnmh0iltfii1p24rpamavswwh2yll\1.0.7321.1900\user.config".

My app also uses NLog for logging, and .json files for storing application data. Those types of files are currently stored in the application directory, but I want to move them to AppData\Local. My preference is to store them in the same folder that contains user.config.

I know how to programmatically get the path to my AppData\Local\EvanscoApps folder, but I do not know how to find "\MyAppName.exe_Url_rtcpnmh0iltfii1p24rpamavswwh2yll". Where does that come from, and how can I access or build it with code?

In the following pseudo code, how should I modify the "get;" for the AppUrlThingy property?

public class LocalDataLocator
{

    public string DataLocation { get; set; }

    public LocalDataLocator()
    {
        DataLocation = GetFolderBase();
    }

    private string LocalFolder { get; }

    private string AppCompany
    {
        get
        {
            var asm = Assembly.GetEntryAssembly();
            var attr = asm.GetCustomAttributes(typeof(AssemblyCompanyAttribute), true);
            return attr.Length > 0 ? ((AssemblyCompanyAttribute) attr[0]).Company : null;
        }
    }

    private Version AppFileVersion
    {
        get
        {
            var asm = Assembly.GetEntryAssembly();
            var attrFileVersion = asm.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true);
            return new Version(((AssemblyFileVersionAttribute) attrFileVersion[0]).Version);
        }
    }

    private ??? AppUrlThingy
    {
        get
        {
            ????????
        }
    }

    private string GetFolderBase()
    {
        var folderBase = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
        var appCompany = AppCompany;
        var appFileVersion = AppFileVersion;
        var appUrlThingy = AppUrlThingy;

        return $@"{folderBase}\{appCompany}\{appUrlThingy}\{appFileVersion}";
    }
}
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,676 questions
0 comments No comments
{count} votes

Accepted answer
  1. R Evans 211 Reputation points
    2020-03-15T22:50:34.273+00:00

    I found an answer to this question on SO. The code below shows my final refactoring to use ConfigurationManager to find the path to the configuration file; which includes the "AppUrlThingy". I hope someone will find this useful.

    using System.Configuration;
    using System.IO;
    
    namespace XWire.DataServices
    {
        public interface ILocalDataLocator
        {
            string DataLocation { get; set; }
        }
    
        public class LocalDataLocator : ILocalDataLocator
        {
            private string AppConfigurationFile
            {
                get
                {
                    var level = ConfigurationUserLevel.PerUserRoamingAndLocal;
                    var configuration = ConfigurationManager.OpenExeConfiguration(level);
                    return configuration.FilePath;
                }
            }
    
            public string DataLocation { get; set; }
    
            private readonly IFileDataService _fileDataService;
    
            public LocalDataLocator(IFileDataService fileDataService)
            {
                _fileDataService = fileDataService;
                InitializeStorageFileLocation();
            }
    
            private void InitializeStorageFileLocation()
            {
                var appConfigurationFile = AppConfigurationFile;
                var appConfigurationFolder = RemoveUserConfigFileName(appConfigurationFile);
                DataLocation = CheckDir(appConfigurationFolder);
            }
    
            private string RemoveUserConfigFileName(string appConfigurationFile)
            {
                return Path.GetDirectoryName(appConfigurationFile);
            }
    
            // Create folder if it is not already there
            private string CheckDir(string appConfigurationFolder)
            {
                if (_fileDataService.DirectoryExists(appConfigurationFolder) == false)
                {
                    _fileDataService.CreateDirectory(appConfigurationFolder);
                }
                return appConfigurationFolder;
            }
        }
    }
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. National Executive Transfers 0 Reputation points
    2024-03-15T08:14:27.8833333+00:00

    The "UserScopedSetting Data Store Location" refers to the location where user-scoped settings data is stored in a software application. User-scoped settings are typically configuration options or preferences that are specific to each user of the application. The data store location could be a file on the user's local system, a registry entry (on Windows), or another storage mechanism depending on the platform and technology used for the application. NET

    0 comments No comments