Accessing App.exe.config File in Special Folders

久未见 提交于 2019-12-11 19:52:02

问题


All, I am doing the usual thing of writing application settings to the 'application.exe.config' file using

Properties.Settings.Default.SomeSetting = someVal;
Properties.Settings.Default.Save();

I have been asked to persist settings between installations and there are two routes; switch to using the registry, or save the .config file to a separate Special Folder that is persisted between installations (I have chosen the later due to number of settings).

My .config gets written to an odd directory, namely

C:\Users\Administrator\AppData\Local\MyApp\
    MyApp.vshost.exe_Url_mzfwtdo5po4pcuabybebhsn5yfltbb3w\1.0.0.0

My question is: how do I pick this directory up in C#?

Note: I have tried

string appPath = Application.ExecutablePath;
Configuration config = ConfigurationManager.OpenExeConfiguration(appPath);
string strIPACostConfigFile = config.FilePath;

Which gives me the initial .config in the installation directory.

Thanks for your time.


回答1:


You don't have to know the location of your config file. You just need a Setting that is true by default and call the following call when your programm starts.

if (Settings.Default.IsUpgrade)
{
  Settings.Default.Upgrade();
  Settings.Default.IsUpgrade = false;
  Settings.Default.Save();
}

That way, settings made in an earlier version will be migrated to the new one.




回答2:


My question is: how do I pick this directory up in C#?

You cannot. The App.exe.config file can be in one of two places, unless you load, generate, and save the configuration file yourself, you will be unable to locate it in the location you want.

Of course the location that Microsoft decided upon is the correct location for it



来源:https://stackoverflow.com/questions/10501036/accessing-app-exe-config-file-in-special-folders

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