Search code examples
c#winformssettings

User settings reset when the application is moved


I've noticed that if you move that application .exe file in another location the user settings reset.

In my understanding this happens because the location in the app data folder is based on the path of the .exe as well, which is taken into consideration when the hash is being generated.

I know there are different alternatives, either by using the registry or by creating manually a custom config file, but my question is, is it possible to retain the user settings when the application (.exe) is moved to another location with the default functionality Microsoft provides ?

For my tests I use a standalone application (one .exe file only). Simply moving the file to, lets say /Desktop/Test/ from /Desktop/ causes a new folder to be generated under C:\Users\<user>\AppData\Local\<company>\Appname_Url_<hash> with the default user.config in it. The old user.config is still available so if you move the .exe file back to its previous location then the previous settings get loaded again. This becomes troublesome not only because you lose the settings, but if you move the file 10 times in 10 different locations you end up with 10 new folders in the appdata.


Solution

  • This is in fact a security feature, related to assembly deployment and clickonce stuff. I think you just need to add a strong name to the main assembly to overcome this issue.

    If you don't have a strong name, the name will be something like this and it will change if the application path change (see the Url token):

    %appdata%\WindowsFormsApplication1\WindowsFormsApplication1._Url_3pei3cdnq3srqpjiwh1qnf12ncsp5c2w\1.0.0.0\user.config
    

    If you have a strong name it will be something like this and it will not change because your app origin is now identified, whatever its current location is (see the StrongName token):

    %appdata%\WindowsFormsApplication1\WindowsFormsApplication1._StrongName_fe0ndyau2vlgeac4gmbg13u3q4jtyrqv\1.0.0.0\user.config