多个应用程序常用的app.config [英] Common app.config for multiple applications

查看:128
本文介绍了多个应用程序常用的app.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个C#控制台应用程序,这需要有一组相同的设置。我想避免表里不一,避免单独的的app.config 为每个应用程序。

I have several C# console applications, which need to have the same set of settings. I want to avoid duplicity and avoid separate app.config for each application.

有没有办法读一个共同的的app.config 文件(比如常见。配置)的应用程序( app1.exe app2.exe )。

Is there any way to read a common app.config file (say common.config) for applications (app1.exe, app2.exe).

推荐答案

您可以用下面的代码加载外部的app.config:

You can load an external app.config using the code below:

config = ConfigurationManager.OpenExeConfiguration(Path.Combine("C:\test\root", "Master.exe"));
string logpath = config.AppSettings.Settings["Log.Path"].Value;

和保存设置像这样:

config = ConfigurationManager.OpenExeConfiguration(Path.Combine("C:\test\root", "Master.exe"));
config.AppSettings.Settings["Log.Path"].Value = "C:\newpath";
config.Save();

您可能必须有一个应用程序内的主配置,其余指向这一点。通常,这种方法虽然被认为是不好的做法。有可能是与不同​​的应用程序锁定文件的问题。

You might have to have a master config within one of the applications and point the rest to this. Typically this method is considered bad practice though. There might be issues with different applications locking the file.

这篇关于多个应用程序常用的app.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆