简体   繁体   English

访问特殊文件夹中的App.exe.config文件

[英]Accessing App.exe.config File in Special Folders

All, I am doing the usual thing of writing application settings to the 'application.exe.config' file using 全部,我正在做的通常是使用应用程序设置写入'application.exe.config'文件

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). 切换到使用注册表,或将.config文件保存到安装之间保留的单独的特殊文件夹(由于设置数量,我选择了后者)。

My .config gets written to an odd directory, namely 我的.config被写入奇数目录,即

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#? 我的问题是:如何在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. 这给了我安装目录中的初始.config。

Thanks for your time. 谢谢你的时间。

My question is: how do I pick this directory up in C#? 我的问题是:如何在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. App.exe.config文件可以位于以下两个位置之一,除非您自己加载,生成和保存配置文件,否则您将无法在所需位置找到它。

Of course the location that Microsoft decided upon is the correct location for it 当然,微软决定的位置是它的正确位置

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. 您只需要一个默认设置为true ,并在程序启动时调用以下调用。

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. 这样,在早期版本中进行的设置将迁移到新版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM