Accessing App.exe.config File in Special Folders

2019-08-16 21:22发布

问题:

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