question

revans611 avatar image
0 Votes"
revans611 asked ·

UserScopedSetting Data Store Location

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-wpf
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

revans611 avatar image
0 Votes"
revans611 answered ·

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;
         }
     }
 }
·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.