C# 将 app.config 文件重定位到自定义路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1838619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Relocating app.config file to a custom path
提问by Ries
Is it possible to relocate the whole App.Config file to a custom path?
是否可以将整个 App.Config 文件重新定位到自定义路径?
It seems a bit odd that the config file resides in the same folder as the exe, with Windows' new approcah of saving all program settings in c:\ProgramData and all.
配置文件与 exe 驻留在同一文件夹中似乎有点奇怪,Windows 的新方法将所有程序设置保存在 c:\ProgramData 和 all 中。
An additional requirement we have is to programatically specify where to find the app.config file. The reason for this being that we spawn different service instances from the same exes, and would like to store each service's app.config in that service's settings folder under c:\ProgramData\\.
我们的另一个要求是以编程方式指定在哪里可以找到 app.config 文件。这样做的原因是我们从相同的 exe 生成不同的服务实例,并且希望将每个服务的 app.config 存储在 c:\ProgramData\\ 下该服务的设置文件夹中。
采纳答案by mYsZa
Each AppDomain has/can have its own configuration file. The default AppDomain created by CLR host uses programname.exe.config; if you want to provide your own configuration file, create separate AppDomain. Example:
每个 AppDomain 都有/可以有自己的配置文件。CLR主机创建的默认AppDomain使用programname.exe.config;如果您想提供自己的配置文件,请创建单独的 AppDomain。例子:
// get the name of the assembly
string exeAssembly = Assembly.GetEntryAssembly().FullName;
// setup - there you put the path to the config file
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationBase = System.Environment.CurrentDirectory;
setup.ConfigurationFile = "<path to your config file>";
// create the app domain
AppDomain appDomain = AppDomain.CreateDomain("My AppDomain", null, setup);
// create proxy used to call the startup method
YourStartupClass proxy = (YourStartupClass)appDomain.CreateInstanceAndUnwrap(
exeAssembly, typeof(YourStartupClass).FullName);
// call the startup method - something like alternative main()
proxy.StartupMethod();
// in the end, unload the domain
AppDomain.Unload(appDomain);
Hope that helps.
希望有帮助。
回答by Adriaan Stander
I am sorry if I misunderstand your request but can you not use
如果我误解了您的要求,我很抱歉,但您不能使用
ConfigurationManager.OpenExeConfiguration Method (String)
ConfigurationManager.OpenExeConfiguration 方法(字符串)
Based on the change, is it possible that you can use
根据更改,您是否可以使用
回答by Tim Robinson
You could use astander's approach of calling OpenExeConfiguration
. If you literally want to relocate your config file, you'll have to create your own app domain. In the process of setting up your app domain you get the chance to specify where the config file is located.
您可以使用 astander 的调用方法OpenExeConfiguration
。如果您确实想重新定位配置文件,则必须创建自己的应用程序域。在设置应用程序域的过程中,您有机会指定配置文件所在的位置。
BTW, .NET config files aren't great for configuration, at least not the sort that users can modify: they're not like INI files or the registry. If you want flexibility over where your configuration comes from you're better off storing it separately.
顺便说一句,.NET 配置文件不适用于配置,至少不是用户可以修改的那种:它们不像 INI 文件或注册表。如果您希望在配置来源方面具有灵活性,最好将其单独存储。
回答by KM?n
MSDNprobably would help...
MSDN可能会有所帮助...
The element simplifies servicing for component assemblies. If one or more applications use an assembly that has a configuration file residing in a well-known location, the configuration files of the applications that use the assembly can use the element to include the assembly configuration file, rather than including configuration information directly. When the component assembly is serviced, updating the common configuration file provides updated configuration information to all applications that use the assembly
该元件简化了对组件组件的维修。如果一个或多个应用程序使用一个程序集,该程序集的配置文件位于众所周知的位置,则使用该程序集的应用程序的配置文件可以使用该元素来包含程序集配置文件,而不是直接包含配置信息。当组件组件得到服务时,更新公共配置文件为使用组件的所有应用程序提供更新的配置信息
回答by wrmsr
This is an ancient question, but I ran into this same problem and came up with a hacky workaround from a few minutes in reflector:
这是一个古老的问题,但我遇到了同样的问题,并从反射器中的几分钟提出了一个hacky解决方法:
static public class ConfigHack {
static public void OverrideAppConfig(string path) {
((AppDomainSetup)
typeof(AppDomain)
.GetField("_FusionStore", BindingFlags.NonPublic | BindingFlags.Instance)
.GetValue(AppDomain.CurrentDomain))
.ConfigurationFile = path;
}
static public void ResetConfigManager() {
typeof(ConfigurationManager)
.GetField("s_initState", BindingFlags.Static | BindingFlags.NonPublic)
.SetValue(null, 0);
}
}
I've only used it on .NET2, but it looks the same in 4 in reflector. Of course I wouldn't recommend shipping this :P I only use it for quick internal things.
我只在 .NET2 上使用过它,但它在反射器中的 4 中看起来是一样的。当然,我不建议运送这个:PI 只将它用于快速的内部事务。
回答by newby
If still relevant, we have used the following I found on another suggested answer to another question here on Stack Overflow...
如果仍然相关,我们已经使用了我在 Stack Overflow 上另一个问题的另一个建议答案中找到的以下内容...
AppDomain.CurrentDomain.SetData ("APP_CONFIG_FILE", "path to config file")
Worked great for us when we had issues loading app.config from DLL only...
当我们在仅从 DLL 加载 app.config 时遇到问题时,对我们来说非常有用......
回答by namford
This worked for me.. (taken from http://msdn.microsoft.com/en-us/library/system.configuration.appsettingssection.aspx)
这对我有用..(取自http://msdn.microsoft.com/en-us/library/system.configuration.appsettingssection.aspx)
// open config
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// update appconfig file path
config.AppSettings.File = "C:\dev\App.config";
// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload in memory of the changed section.
ConfigurationManager.RefreshSection("appSettings");
Then when you call
然后当你打电话
NameValueCollection settings = System.Configuration.ConfigurationManager.AppSettings;
or any operation to fetch the app config, the new path is used.
或任何获取应用程序配置的操作,都会使用新路径。
Hope this might help someone else who has same problem!
希望这可以帮助其他有同样问题的人!
回答by bas
I know this is an old question, but for those who justwant to have their app.config in a different location then their binary output build location, the following works as microsoft intended it (and thus no need re-read and re-write the file to disk)
我知道这是一个老问题,但对于那些谁只是希望有自己不同的位置,然后他们的二进制输出生成位置的app.config,下面的作品如微软预期它(因此,无需重新读取和重新写入文件到磁盘)
- Define an app.config as you always do.
- Define another config file where you want to have the actual configuration file
- Change the app.config so that it refers to the configuration file
- 像往常一样定义 app.config。
- 定义另一个配置文件,您希望在其中拥有实际的配置文件
- 更改 app.config 使其引用配置文件
At runtime the settings from the configuration file will override the settings in the app.config (if present). And you are done.
在运行时,配置文件中的设置将覆盖 app.config 中的设置(如果存在)。你已经完成了。
Example app.config
示例 app.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<appSettings file="..\Config\settings.config">
<add key="port" value="1001"/>
</appSettings>
</configuration>
Note the file="..\Config\settings.config". You are completely free to define the path to the location where you want your users to change settings.
注意file="..\Config\settings.config"。您可以完全自由地定义您希望用户更改设置的位置的路径。
Example of the actual configuration file
实际配置文件示例
<?xml version="1.0" encoding="utf-8"?>
<appSettings>
<add key="port" value="1234"/>
</appSettings>
At runtime the setting port
will have the value 1234.
在运行时,该设置port
的值为 1234。
More info see msdn
更多信息见msdn
回答by Mhmetengineer
If u use the registry, your problem can be resolved.
如果您使用注册表,则可以解决您的问题。