亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > C# > 正文

C#讀寫config配置文件的方法

2019-10-29 21:18:16
字體:
來源:轉載
供稿:網友

如下所示:

<?xml version="1.0" encoding="utf-8" ?><configuration> <appSettings>  <add key="ServerIP" value="127.0.0.1"></add>  <add key="DataBase" value="WarehouseDB"></add>  <add key="user" value="sa"></add>  <add key="password" value="sa"></add> </appSettings></configuration>

對config配置文件的讀寫類

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Text.RegularExpressions;using System.Configuration;using System.ServiceModel;using System.ServiceModel.Configuration;namespace NetUtilityLib{  public static class ConfigHelper  {    //依據連接串名字connectionName返回數據連接字符串     public static string GetConnectionStringsConfig(string connectionName)    {      //指定config文件讀取      string file = System.Windows.Forms.Application.ExecutablePath;      System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(file);      string connectionString =        config.ConnectionStrings.ConnectionStrings[connectionName].ConnectionString.ToString();      return connectionString;    }    ///<summary>     ///更新連接字符串     ///</summary>     ///<param name="newName">連接字符串名稱</param>     ///<param name="newConString">連接字符串內容</param>     ///<param name="newProviderName">數據提供程序名稱</param>     public static void UpdateConnectionStringsConfig(string newName, string newConString, string newProviderName)    {      //指定config文件讀取      string file = System.Windows.Forms.Application.ExecutablePath;      Configuration config = ConfigurationManager.OpenExeConfiguration(file);      bool exist = false; //記錄該連接串是否已經存在       //如果要更改的連接串已經存在       if (config.ConnectionStrings.ConnectionStrings[newName] != null)      {        exist = true;      }      // 如果連接串已存在,首先刪除它       if (exist)      {        config.ConnectionStrings.ConnectionStrings.Remove(newName);      }      //新建一個連接字符串實例       ConnectionStringSettings mySettings =        new ConnectionStringSettings(newName, newConString, newProviderName);      // 將新的連接串添加到配置文件中.       config.ConnectionStrings.ConnectionStrings.Add(mySettings);      // 保存對配置文件所作的更改       config.Save(ConfigurationSaveMode.Modified);      // 強制重新載入配置文件的ConnectionStrings配置節       ConfigurationManager.RefreshSection("ConnectionStrings");    }    ///<summary>     ///返回*.exe.config文件中appSettings配置節的value項     ///</summary>     ///<param name="strKey"></param>     ///<returns></returns>     public static string GetAppConfig(string strKey)    {      string file = System.Windows.Forms.Application.ExecutablePath;      Configuration config = ConfigurationManager.OpenExeConfiguration(file);      foreach (string key in config.AppSettings.Settings.AllKeys)      {        if (key == strKey)        {          return config.AppSettings.Settings[strKey].Value.ToString();        }      }      return null;    }    ///<summary>     ///在*.exe.config文件中appSettings配置節增加一對鍵值對     ///</summary>     ///<param name="newKey"></param>     ///<param name="newValue"></param>     public static void UpdateAppConfig(string newKey, string newValue)    {      string file = System.Windows.Forms.Application.ExecutablePath;      Configuration config = ConfigurationManager.OpenExeConfiguration(file);      bool exist = false;      foreach (string key in config.AppSettings.Settings.AllKeys)      {        if (key == newKey)        {          exist = true;        }      }      if (exist)      {        config.AppSettings.Settings.Remove(newKey);      }      config.AppSettings.Settings.Add(newKey, newValue);      config.Save(ConfigurationSaveMode.Modified);      ConfigurationManager.RefreshSection("appSettings");    }    // 修改system.serviceModel下所有服務終結點的IP地址    public static void UpdateServiceModelConfig(string configPath, string serverIP)    {      Configuration config = ConfigurationManager.OpenExeConfiguration(configPath);      ConfigurationSectionGroup sec = config.SectionGroups["system.serviceModel"];      ServiceModelSectionGroup serviceModelSectionGroup = sec as ServiceModelSectionGroup;      ClientSection clientSection = serviceModelSectionGroup.Client;      foreach (ChannelEndpointElement item in clientSection.Endpoints)      {        string pattern = @"/b/d{1,3}/./d{1,3}/./d{1,3}/./d{1,3}/b";        string address = item.Address.ToString();        string replacement = string.Format("{0}", serverIP);        address = Regex.Replace(address, pattern, replacement);        item.Address = new Uri(address);      }      config.Save(ConfigurationSaveMode.Modified);      ConfigurationManager.RefreshSection("system.serviceModel");    }    // 修改applicationSettings中App.Properties.Settings中服務的IP地址    public static void UpdateConfig(string configPath, string serverIP)    {      Configuration config = ConfigurationManager.OpenExeConfiguration(configPath);      ConfigurationSectionGroup sec = config.SectionGroups["applicationSettings"];      ConfigurationSection configSection = sec.Sections["DataService.Properties.Settings"];      ClientSettingsSection clientSettingsSection = configSection as ClientSettingsSection;      if (clientSettingsSection != null)      {        SettingElement element1 = clientSettingsSection.Settings.Get("DataService_SystemManagerWS_SystemManagerWS");        if (element1 != null)        {          clientSettingsSection.Settings.Remove(element1);          string oldValue = element1.Value.ValueXml.InnerXml;          element1.Value.ValueXml.InnerXml = GetNewIP(oldValue, serverIP);          clientSettingsSection.Settings.Add(element1);        }        SettingElement element2 = clientSettingsSection.Settings.Get("DataService_EquipManagerWS_EquipManagerWS");        if (element2 != null)        {          clientSettingsSection.Settings.Remove(element2);          string oldValue = element2.Value.ValueXml.InnerXml;          element2.Value.ValueXml.InnerXml = GetNewIP(oldValue, serverIP);          clientSettingsSection.Settings.Add(element2);        }      }      config.Save(ConfigurationSaveMode.Modified);      ConfigurationManager.RefreshSection("applicationSettings");    }    private static string GetNewIP(string oldValue, string serverIP)    {      string pattern = @"/b/d{1,3}/./d{1,3}/./d{1,3}/./d{1,3}/b";      string replacement = string.Format("{0}", serverIP);      string newvalue = Regex.Replace(oldValue, pattern, replacement);      return newvalue;    }  }}

測試代碼如下:

class Program  {    static void Main(string[] args)    {      try      {        //string file = System.Windows.Forms.Application.ExecutablePath + ".config";        //string file1 = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;        string serverIP = ConfigHelper.GetAppConfig("ServerIP");        string db = ConfigHelper.GetAppConfig("DataBase");        string user = ConfigHelper.GetAppConfig("user");        string password = ConfigHelper.GetAppConfig("password");        Console.WriteLine(serverIP);        Console.WriteLine(db);        Console.WriteLine(user);        Console.WriteLine(password);        ConfigHelper.UpdateAppConfig("ServerIP", "192.168.1.11");        string newIP = ConfigHelper.GetAppConfig("ServerIP");        Console.WriteLine(newIP);        Console.ReadKey();      }      catch (Exception ex)      {        Console.WriteLine(ex.Message);      }    }  }

以上這篇C#讀寫config配置文件的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
国产在线999| 国产欧美精品日韩| 亚洲a一级视频| 国产成+人+综合+亚洲欧洲| 91av在线国产| 日韩黄色高清视频| 91av在线精品| 国产日韩在线精品av| 日韩欧美高清在线视频| 欧美福利视频在线| 亚洲国产精品成人va在线观看| 色综合91久久精品中文字幕| 88国产精品欧美一区二区三区| 88国产精品欧美一区二区三区| 日产精品久久久一区二区福利| 欧美激情一区二区三区在线视频观看| 久久久这里只有精品视频| 欧美小视频在线观看| 国产精品久久久久久久久久三级| 色婷婷综合久久久久| 欧美日韩黄色大片| 亚洲高清av在线| 欧美亚洲国产成人精品| 在线日韩日本国产亚洲| 亚洲欧美精品伊人久久| 精品av在线播放| 国产免费久久av| 久久精品99久久久久久久久| 亚洲最新在线视频| 欧美日韩激情小视频| 国产精品亚洲网站| 92看片淫黄大片欧美看国产片| 日韩精品免费一线在线观看| 成人黄色免费片| 久久精品免费电影| 国产亚洲美女久久| 高清亚洲成在人网站天堂| 亚洲娇小xxxx欧美娇小| 欧美日韩午夜激情| 亚洲已满18点击进入在线看片| 中文字幕在线精品| 亚洲国产一区自拍| 亚洲天天在线日亚洲洲精| 欧美日产国产成人免费图片| 亚洲图片制服诱惑| 亚洲欧美日韩国产成人| 国产在线观看一区二区三区| 亚洲精品国偷自产在线99热| 主播福利视频一区| 日韩成人激情在线| 欧美视频专区一二在线观看| 在线观看日韩www视频免费| 欧美在线观看网站| 性欧美视频videos6一9| 日韩中文字幕在线免费观看| 福利一区福利二区微拍刺激| 日韩精品免费在线视频| 亚洲精品色婷婷福利天堂| 91精品视频在线| 久久久久久网址| 国产精品久久久精品| 91人成网站www| 91精品国产91久久久| 日韩免费在线观看视频| 欧美在线亚洲在线| 日韩中文字幕在线精品| 黑人极品videos精品欧美裸| 国产亚洲精品美女久久久久| 亚洲va久久久噜噜噜| 精品国产一区二区三区久久久| 久久av在线看| 久久频这里精品99香蕉| 久久国产精品免费视频| 亚洲精品一区中文字幕乱码| 亚洲人成在线观| 国产精品电影观看| 国产一区二区三区日韩欧美| 日韩av日韩在线观看| 青青久久av北条麻妃黑人| 亚洲国产日韩欧美综合久久| 精品日本高清在线播放| 久久久久www| 国产啪精品视频| 亚洲va欧美va在线观看| 久久久天堂国产精品女人| 欧美韩国理论所午夜片917电影| 国产精品美女久久久久av超清| 亚洲欧美日本伦理| 久久人人爽人人爽人人片av高清| 亚洲国产高清自拍| 91嫩草在线视频| 亚洲人午夜色婷婷| 亚洲人在线视频| 欧美精品手机在线| 91久久久国产精品| 中文字幕亚洲欧美日韩在线不卡| 欧美人与性动交a欧美精品| 亚洲自拍偷拍网址| 欧美电影院免费观看| 91禁外国网站| 久久伊人精品一区二区三区| 精品自在线视频| 91精品啪在线观看麻豆免费| 97精品欧美一区二区三区| 最新亚洲国产精品| 亚洲午夜av久久乱码| 国产福利成人在线| 久久夜精品香蕉| 欧美裸体xxxx| 中文字幕日本欧美| 欧美成人一区二区三区电影| 久久天堂电影网| 欧美成人免费小视频| 欧美高清视频在线观看| 国产欧美精品一区二区| 九色精品美女在线| 久久久精品美女| 久久综合伊人77777蜜臀| 国产99久久久欧美黑人| 亚洲欧美日韩网| 国产精品久久久久一区二区| 欧美诱惑福利视频| 日韩精品亚洲精品| 国产欧美日韩免费看aⅴ视频| 久久的精品视频| 免费91麻豆精品国产自产在线观看| 26uuu另类亚洲欧美日本老年| 日韩精品极品在线观看播放免费视频| 亚洲电影成人av99爱色| 国内外成人免费激情在线视频| 国产免费一区二区三区在线能观看| 亚洲精品自拍偷拍| 欧美高清不卡在线| 日韩电影中文字幕| 久久久久久久久久久久久久久久久久av| 国产成人精品日本亚洲专区61| 欧洲美女7788成人免费视频| 91精品国产91久久久久久吃药| 亚洲福利视频二区| 欧美一区二区三区……| 91嫩草在线视频| 国产偷国产偷亚洲清高网站| 国产男人精品视频| 国产欧美一区二区白浆黑人| 亚洲精品资源美女情侣酒店| 国产精品久久久久免费a∨| 欧美激情精品在线| 5566日本婷婷色中文字幕97| 91久久精品久久国产性色也91| 欧洲精品久久久| 国产日韩在线免费| 欧洲成人在线观看| 日韩三级成人av网| 91av在线播放视频| 高跟丝袜一区二区三区| 欧美日在线观看| 国产精品自产拍在线观| 亚洲成av人乱码色午夜| 亚洲乱亚洲乱妇无码| 亚洲精品久久7777777| 最近免费中文字幕视频2019| 欧美黑人巨大精品一区二区| www.久久久久| 国产精品成人aaaaa网站|