David
David

Reputation: 4117

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

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?

Upvotes: 4

Views: 9122

Answers (2)

Angelo Mascaro
Angelo Mascaro

Reputation: 129

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.

Upvotes: 4

Related Questions