2

Hi I'm using the Visual Studio config files, however the settings change every time I move the exe.

How can I fix this?

app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="GUIChangerUI.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
    <userSettings>
        <GUIChangerUI.Properties.Settings>
            <setting name="StarmadePath" serializeAs="String">
                <value>default</value>
            </setting>
            <setting name="GuiPath" serializeAs="String">
                <value>Not selected yet.</value>
            </setting>
            <setting name="FirstStart" serializeAs="String">
                <value>True</value>
            </setting>
            <setting name="jpeg" serializeAs="String">
                <value>default</value>
            </setting>
            <setting name="debug" serializeAs="String">
                <value>default</value>
            </setting>
            <setting name="Darktheme" serializeAs="String">
                <value>False</value>
            </setting>
            <setting name="Lightheme" serializeAs="String">
                <value>True</value>
            </setting>
            <setting name="starmadeStarter" serializeAs="String">
                <value />
            </setting>
            <setting name="_starmadeStarter" serializeAs="String">
                <value />
            </setting>
            <setting name="OSMTheme" serializeAs="String">
                <value>False</value>
            </setting>
        </GUIChangerUI.Properties.Settings>
    </userSettings>
</configuration>
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Agronaut022
  • 79
  • 1
  • 7

3 Answers3

5

The actual configuration file containing saved configuration settings is stored here:

%APPDATA%\Local\<application name>\<application name>.<eid>_<hash>\<version>

According to this MSDN article::

<eid> is the URL, StrongName, or Path, based on the evidence available to hash.
<hash> is a SHA1 hash of evidence gathered from the CurrentDomain, in the following order of preference:
StrongName
URL
If neither of these is available, use the .exe path.

(my emphasis)

So the solution seems simple:

Create a strong name and sign your executable.

Then you will get the same unique hash every time and it won't change whenever you start the executable from a new location.

If you need help signing your application, please refer to this MSDN article: How to: Sign an Assembly with a Strong Name.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • Thats the right answer. It will solve the OPs problem (moving exe file). However, if the version of the application changes, the settings will be lost again, right? – JanDotNet May 13 '16 at 11:55
  • 1
    He must call `Upgrade` on the settings, there is another answer here that details how to do this and though that answer doesn't answer why the settings are cleared/forgotten when the executable is moved, he needs to handle this case as well. – Lasse V. Karlsen May 13 '16 at 11:57
  • Okay this works perfectly <3, but i had to sign some dlls i had with my key. – Agronaut022 May 13 '16 at 13:15
  • Note that this path (and its explanation) is outdated as of later .NET versions. See https://stackoverflow.com/q/982354/1688738 for more. – Hugh W Jan 27 '23 at 15:26
1

That behavior is by design because you could have multiple versions of you application (for instance a QA version, a PROD version and so on) that requires different setting storages. See also Client Settings FAQ for details.

If you need a settings management that is independend of the location / version of you app, i would suggest wo create your own settings file and store them below "%appdata%[company][application]"

JanDotNet
  • 3,746
  • 21
  • 30
1

The user settings will be stored in your user's profile in such a way that the location of your application is linked. That's why the application, when moved, does not find the settings anymore.

What you can try to do is:

  1. Create a new user setting named SettingsUpgradeRequired and set it to true in the settings designer in Visual Studio.
  2. In your application's startup code, check whether SettingsUpgradeRequired is true and if so, perform a settings upgrade.

As the new setting will only be true after the settings file was reset, the following should import the old settings, and it should do so only once:

if (Properties.Settings.Default.SettingsUpgradeRequired)
{
    try
    {
        Properties.Settings.Default.Upgrade();
        Properties.Settings.Default.SettingsUpgradeRequired = false;
        Properties.Settings.Default.Save();
    }
    catch (...)
    {
       ... // Upgrade failed - tell the user or whatever
    }
}
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139