C# 读取和修改指定config文件内的配置

由于修改默认的app.config文件,总是会在执行完后被旧文件覆盖,不清楚是什么原因。所以替代办法是在另外一个自定义的配置文件中进行读写。

1.读取

//读取Debug文件夹下自定义的PrintSetting.config中的配置信息
        private string getConfig(string key)
        {
            string fileName = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\PrintSetting.config";
            ExeConfigurationFileMap map = new ExeConfigurationFileMap();
            map.ExeConfigFilename = fileName;
            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
            return config.AppSettings.Settings[key].Value == null ? "" : config.AppSettings.Settings[key].Value;
        }

2.修改

//修改配置信息
        private void setConfig(string key, string value)
        {
            XmlDocument xml = new XmlDocument();
            string fileName = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\PrintSetting.config";
            xml.Load(fileName);
            XmlNodeList nodes = xml.GetElementsByTagName("add");
            for (int i = 0; i < nodes.Count; i++)
            {
                XmlAttribute att = nodes[i].Attributes["key"];
                if (att.Value == key)
                {
                    att = nodes[i].Attributes["value"];
                    att.Value = value;
                    break;
                }
            }
            xml.Save(fileName);
            System.Configuration.ConfigurationManager.RefreshSection("appSettings");
        }
时间: 2024-08-01 21:30:48

C# 读取和修改指定config文件内的配置的相关文章

C# 获取或设置指定 config 文件的值

ExeConfigurationFileMap 这个类提供了修改.获取指定 config 的功能:新建一个 ExeConfigurationFileMap 的实例 ecf :并设置 ExeConfigFilename 属性为要操作的 config 文件路径:使用 ConfigurationManager.OpenMappedExeConfiguration 方法得到操对象 Configuration config调用 Configuration 对象实例提供的 config.AppSetting

config文件中可以配置查询超时时间

web.config配置数据库连接 第一种:获取连接字符串 首先要定义命名空间 system.configuration 1.  string connstr= string constr = ConfigurationManager.AppSettings["connstring"]; web.config文件:加在<appsettings>和</appsettings> 之间 <appsettings> <add key="con

C#读取指定config文件

参考http://stackoverflow.com/questions/1682054/how-do-i-retrieve-appsettings-from-the-assembly-config-file ExeConfigurationFileMap map = new ExeConfigurationFileMap(); map.ExeConfigFilename = "SomeApp.config"; Configuration libConfig = Configurati

Winform後台如何動態修改App.config文件里的內容

以下方法修改的,自己添加的app.config裡面不會顯示出修改的東西. 方法一:通過使用System.Xml.XmlDocument對象的方法進行bin\debug\~.vshost.exe.Config裡面的配置修改.(這種方法在程式下次啟動時才會生效,直到你清除項目重建 或是重新手動修改才會恢復為你自己寫的配置信息) public static void SetValue(string AppKey, string AppValue)        {            System.

asp.net修改web.config文件

private void UpdateConfigFile() { var cfg = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); if (cfg.ConnectionStrings.ConnectionStrings["masterConnString"] == null) { var elCon = cfg.ConnectionStri

php网站修改默认访问文件的nginx配置

搭建好lnmp后,有时候并不需要直接访问index.php,配置其他的默认访问文件比如index.html这时候需要配置一下nginx才能访问到你想要设置的文件 直接上代码,如下是我的配置的一份简单的nginx到php-fpm的站点,该站点默认访问目录/ecmoban/www/index.html server { listen 80; location / { root /ecmoban/www; index index.html index.php index.htm; } error_pa

关于 App.config文件出错,配置系统未能初始化。 问题解决方案

如果配置文件中包含 configSections 元素,则 configSections 元素必须是 configuration 元素的第一个子元素.将appSettings放到configSections 后,则正常. configSections中的元素必须和下面的自定义配置节一一对应. 例如 下面有4个自定义配置节,但是在configSections只有3个,就会出错:配置系统未能初始化. <configSections>   <section name="A_F63_S

ASP.NET一个网站内存放多个config文件(Web.Config文件中configSource

每个网站里都会有一个web.config文件.修改Web.config文件会导致IIS重启,就是随意的回车一下也会导致重启.微软建议,不要将需要修改的配置内容保存在web.config中.而是单独放在一个config中.但是对于单独存放的config文件,怎么来对其进行修改和读取呢? 例如 可以指定 web.config 中的 appSetting 单独放在 一个 config.config 文件中.通过 configSource 来指定. 一.原来的web.config文件: <?xml ve

在.net中读写config文件的各种方法(自定义config节点)

http://www.cnblogs.com/fish-li/archive/2011/12/18/2292037.html 阅读目录 开始 config文件 - 自定义配置节点 config文件 - Property config文件 - Element config文件 - CDATA config文件 - Collection config文件 - 读与写 读写 .net framework中已经定义的节点 xml配置文件 xml配置文件 - CDATA xml文件读写注意事项 配置参数的