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

首頁 > 編程 > C# > 正文

詳解C#如何讀寫config配置文件

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

配置文件概述:

應用程序配置文件是標準的 XML 文件,XML 標記和屬性是區分大小寫的。它是可以按需要更改的,開發人員可以使用配置文件來更改設置,而不必重編譯應用程序。配置文件的根節點是configuration。我們經常訪問的是appSettings,它是由.Net預定義的配置節。我們經常使用的配置文件的架構是客訴下面的形式。先大概有個印象,通過后面的實例會有一個比較清楚的認識。下面的“配置節”可以理解為進行配置一個XML的節點。

對于一個config文件:

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
久久国产一区二区三区| 成人av在线亚洲| 亚洲第一网站男人都懂| 久久乐国产精品| 亚洲国产美女精品久久久久∴| 欧美电影第一页| 久久影视电视剧免费网站清宫辞电视| 国产精品专区第二| 欧美黄色三级网站| 国产成人av网址| 国产精品美女久久久免费| 92国产精品久久久久首页| 亚洲国产精品中文| 最近2019年日本中文免费字幕| 欧美日韩国产精品一区| 欧美激情精品久久久久久| 一本色道久久88亚洲综合88| 在线视频国产日韩| 在线播放日韩精品| 亚洲欧洲午夜一线一品| 日韩美女激情视频| 国产精品一区二区久久精品| 亚洲综合在线小说| 亚洲精品中文字| 国产精品久久久久久久久久| 亚洲精品999| 欧美三级xxx| 色噜噜狠狠狠综合曰曰曰| 日本韩国欧美精品大片卡二| 欧美成人免费播放| 45www国产精品网站| 久久精品国产亚洲精品| 欧美一级淫片videoshd| 亚洲精品国产美女| 欧美亚洲成人精品| 91精品视频免费看| 亚洲jizzjizz日本少妇| 亚洲大胆人体av| 91中文字幕在线观看| 亚洲国产欧美一区二区丝袜黑人| 国产精品一区二区三区在线播放| 亚洲免费成人av电影| 久久精品99久久香蕉国产色戒| 在线成人一区二区| 亚洲tv在线观看| 国产激情综合五月久久| 日韩免费在线视频| 91精品国产自产在线观看永久| 久久久精品一区二区| 成人做爰www免费看视频网站| 狠狠做深爱婷婷久久综合一区| 91香蕉国产在线观看| 日韩第一页在线| 黑人巨大精品欧美一区免费视频| 77777亚洲午夜久久多人| 97在线看福利| 狠狠色噜噜狠狠狠狠97| 亚洲视频综合网| 欧美精品免费看| 孩xxxx性bbbb欧美| 精品久久久中文| 亚洲影院在线看| 久久精品国产精品| 亚洲国产精品成人精品| 国产视频丨精品|在线观看| 亚洲女人天堂色在线7777| 日韩风俗一区 二区| 久久综合网hezyo| 91中文在线视频| 亚洲精品一区av在线播放| 欧美视频中文字幕在线| 美女999久久久精品视频| 欧美激情亚洲视频| 久久综合电影一区| 国产精品美乳在线观看| 91精品国产91久久久久久| 日韩在线视频免费观看| 久久久久久久一区二区| 亚洲人精品午夜在线观看| 久久视频精品在线| 欧美精品成人在线| 国产成人精品国内自产拍免费看| 国产精品久久久久久久app| 欧美成在线观看| 久久精品国产69国产精品亚洲| 久久久精品国产| 日韩成人在线观看| 久久久久久久影视| 国产精品久久久久久久久男| 欧美日本亚洲视频| 亚洲精品国产精品国自产观看浪潮| 久久成年人免费电影| 国产国语刺激对白av不卡| 欧美肥老妇视频| 久久99视频免费| 性色av一区二区三区红粉影视| 欧美疯狂做受xxxx高潮| 亚洲成人激情在线观看| 国产日产欧美a一级在线| 久久久亚洲影院你懂的| 亚洲片国产一区一级在线观看| 亚洲欧美日韩在线高清直播| 欧美亚洲激情视频| 这里只有视频精品| 国产精品久久视频| 国产精品久久久久久超碰| 97免费视频在线| 久久亚洲精品一区| 国产视频精品免费播放| 欧美福利视频在线观看| 国产成人精品a视频一区www| 欧美俄罗斯性视频| 欧美激情国产高清| 亚洲香蕉成视频在线观看| 亚洲韩国日本中文字幕| 亚洲欧美制服丝袜| 永久免费毛片在线播放不卡| 国产精品96久久久久久| 性色av一区二区三区免费| 97人人爽人人喊人人模波多| 国产精品对白刺激| 国产99在线|中文| 色噜噜久久综合伊人一本| 一本色道久久88综合亚洲精品ⅰ| 亚洲色图35p| 亚洲欧美在线免费观看| 亚洲黄色在线观看| 欧美精品第一页在线播放| 色噜噜国产精品视频一区二区| 欧美精品日韩www.p站| 日韩中文视频免费在线观看| 欧美激情免费观看| 亚洲精品美女在线观看| 成人欧美一区二区三区在线湿哒哒| 久国内精品在线| 国产精品美女999| 亚洲激情在线视频| 日韩电影免费观看在线| 日韩高清电影好看的电视剧电影| 欧美精品在线网站| 欧美成人午夜免费视在线看片| 一色桃子一区二区| 91欧美精品成人综合在线观看| 欧美激情亚洲自拍| 亚洲第一av网站| 国产精品久久久久久久电影| 欧美理论电影网| 亚洲影院色在线观看免费| 亚洲大胆美女视频| 亚洲欧美精品中文字幕在线| 日韩中文字幕久久| 国产精品大陆在线观看| 日韩女在线观看| 成人午夜在线影院| 亚洲精品久久视频| 色婷婷综合久久久久| 亚洲一区www| 福利视频导航一区| 亚洲欧美日韩中文视频| 国产97人人超碰caoprom| 国产成人免费av| 中文字幕免费精品一区高清| 国产亚洲精品久久久久动| 国产视频久久网|