User settings reset when the application is moved

空扰寡人 提交于 2019-12-07 14:39:43

问题


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.


回答1:


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



回答2:


From this blog post, regarding the location of the configuration file:

If you need to store the settings in a different location for some reason, the recommended way is to write your own SettingsProvider.

Which tells me that with a little bit of code, you could probably store the file right where you want it, and it won't move.



来源:https://stackoverflow.com/questions/16759018/user-settings-reset-when-the-application-is-moved

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!