Properties.Settings.Default.Save() store the user.config file in a directory with a hash

David picture David · Jul 4, 2012 · Viewed 8.5k times · Source

I try to save user settings. To save some data I used this code:

Properties.Settings settings = Properties.Settings.Default;
settings.Key1 = "value";
settings.Save();

but it saves the user.config file under the following path:

C:\Users\Me\AppData\Local\[CompanyName]\[ExeName]_Url_[some_hash]\[Version]\user.config

this with the _Url_[some_hash] is pretty ugly, how can I remove it?

Answer

Angelo Mascaro picture Angelo Mascaro · May 1, 2015

You can! Just follow this article that explain everything in full details, then you have to modify the property UserConfigPath as follows:

        private string UserConfigPath
    {
        get
        {
            System.Diagnostics.FileVersionInfo versionInfo;
            string strUserConfigPath, strUserConfigFolder;

            strUserConfigPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData, Environment.SpecialFolderOption.Create);
            versionInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(Assembly.GetEntryAssembly().Location);
            strUserConfigPath = Path.Combine(strUserConfigPath, versionInfo.CompanyName, versionInfo.ProductName, versionInfo.ProductVersion, "user.config");
            strUserConfigFolder = Path.GetDirectoryName(strUserConfigPath);
            if(!Directory.Exists(strUserConfigFolder))
                Directory.CreateDirectory(strUserConfigFolder);
            return strUserConfigPath;
        }
    }

In this way you build the path from scratch. You should also modify the method CreateEmptyConfig in order to have a default action when the user.config file is not found.