将app.config文件重定位到自定义路径

问题描述 投票:31回答:9

是否可以将整个App.Config文件重定位到自定义路径?

[配置文件与exe驻留在同一文件夹中似乎有点奇怪,Windows的新做法是将所有程序设置保存在c:\ ProgramData中,以及全部。

我们还有一个附加要求,就是以编程方式指定在哪里可以找到app.config文件。原因是我们从相同的exe生成了不同的服务实例,并希望将每个服务的app.config存储在该服务的settings文件夹中的c:\ ProgramData \\下。

c# .net path app-config
9个回答
18
投票

每个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);

希望有所帮助。


39
投票

[如果仍然相关,我们将以下内容用于我对堆栈溢出的另一个问题的另一个建议答案中。

AppDomain.CurrentDomain.SetData ("APP_CONFIG_FILE", "path to config file")

当我们遇到仅从DLL加载app.config的问题时,对我们非常有用...


7
投票

这对我有用..(摘自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");

然后打电话时

NameValueCollection settings = System.Configuration.ConfigurationManager.AppSettings;

或任何用于获取应用程序配置的操作,将使用新路径。

希望这可以帮助遇到相同问题的其他人!


7
投票

我知道这是一个古老的问题,但是对于那些[[just想要将其app.config放在其他位置然后位于其二进制输出生成位置的人,以下功能可以按照Microsoft的预期进行(因此无需重新安装) -读取并将文件重新写入磁盘)

    一如既往地定义app.config。
  • 在要具有实际配置文件的位置定义另一个配置文件
  • 更改app.config,使其引用配置文件
  • 在运行时,配置文件中的设置将覆盖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>

    注意

    file =“ .. \ Config \ settings.config”。您完全可以自由定义要用户更改设置的位置的路径。

    实际配置文件示例

    <?xml version="1.0" encoding="utf-8"?> <appSettings> <add key="port" value="1234"/> </appSettings>

    在运行时,设置port的值为1234。

    更多信息,请参见msdn


  • 2
    投票
    这是一个古老的问题,但是我遇到了同样的问题,并在反射器中花了几分钟的时间才提出了一个棘手的解决方法:

    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); } }

    我只在.NET2上使用过它,但是在反射器中它在4中看起来相同。当然,我不建议运送此:P我只将其用于快速内部操作。

    1
    投票
    抱歉,如果我误解了您的请求,但您不能使用

    ConfigurationManager.OpenExeConfiguration Method (String)

    根据更改,可以使用

    AppDomainSetup.ConfigurationFile Property


    1
    投票
    您可以使用旁观者的方法呼叫OpenExeConfiguration。如果您确实要重定位配置文件,则必须创建自己的应用程序域。在设置应用程序域的过程中,您可以指定配置文件的位置。

    BTW,.NET配置文件不适用于配置,至少不是用户可以修改的类型:它们不像INI文件或注册表。如果您想灵活了解配置的来源,最好将其单独存储。


    0
    投票
    如果您使用注册表,则可以解决您的问题。

    -3
    投票
    MSDN可能会帮助...

    元素简化了组件的维修组件。如果一个或多个应用程序使用具有驻留在众所周知的位置,配置使用以下应用程序的文件组装可以使用元素包括装配配置文件,而不是包含直接配置信息。当组件装配是服务,更新通用配置文件提供更新所有人的配置信息使用该程序集的应用程序
    © www.soinside.com 2019 - 2024. All rights reserved.