以下是读取配置文件信息方法 引用程序集System.Configuration
public class config { public static string Token = ""; public static string user = GetAppConfig("User"); //指定config文件读取 private static string file = GetFilePath(); private static string GetFilePath() { return System.Windows.Forms.Application.ExecutablePath; } ///<summary> ///返回*.exe.config文件中appSettings配置节的value项 ///</summary> ///<param name="strKey"></param> ///<returns></returns> public static string GetAppConfig(string strKey) { Configuration config = ConfigurationManager.OpenExeConfiguration(file); return config.AppSettings.Settings[strKey].Value; } /// <summary> /// 依据连接串名字connectionName返回数据连接字符串 /// </summary> /// <param name="connectionName"></param> /// <returns></returns> public static string GetConnectionStringsConfig(string connectionName) { Configuration config = ConfigurationManager.OpenExeConfiguration(file); return config.ConnectionStrings.ConnectionStrings[connectionName].ConnectionString.ToString(); } ///<summary> ///在*.exe.config文件中appSettings配置节增加一对键值对 ///</summary> ///<param name="newKey"></param> ///<param name="newValue"></param> public static void UpdateAppConfig(string newKey, string newValue) { 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"); } ///<summary> ///更新连接字符串 ///</summary> ///<param name="newName">连接字符串名称</param> ///<param name="newConString">连接字符串内容</param> ///<param name="newProviderName">数据提供程序名称</param> public static void UpdateConnectionStringsConfig(string newName, string newConString, string newProviderName) { 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> /// 修改system.serviceModel下所有服务终结点的IP地址 /// 只改ip 不改端口 /// </summary> /// <param name="serverIP">127.0.0.1</param> public static void UpdateServiceModelConfig(string serverIP) { Configuration config = ConfigurationManager.OpenExeConfiguration(file); ConfigurationSectionGroup sec = config.SectionGroups["system.serviceModel"]; ServiceModelSectionGroup serviceModelSectionGroup = sec as ServiceModelSectionGroup; ClientSection clientSection = serviceModelSectionGroup.Client; foreach (ChannelEndpointElement item in clientSection.Endpoints) { string pattern = @"d{1,3}.d{1,3}.d{1,3}.d{1,3}"; 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"); } /// <summary> /// 加密 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public static void Encryption() { Configuration config = ConfigurationManager.OpenExeConfiguration(file); ConfigurationSection section = config.GetSection("connectionStrings"); if (section != null && !section.SectionInformation.IsProtected) { //section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider"); section.SectionInformation.ProtectSection("RSAProtectedConfigurationProvider"); config.Save(); } ConfigurationSection section1 = config.GetSection("appSettings"); if (section1 != null && !section1.SectionInformation.IsProtected) { // section1.SectionInformation.ProtectSection("DataProtectionConfigurationProvider"); section1.SectionInformation.ProtectSection("RSAProtectedConfigurationProvider"); config.Save(); } } /// <summary> /// 解密 /// </summary> public static void Decrypt() { Configuration config = ConfigurationManager.OpenExeConfiguration(file); ConfigurationSection section = config.GetSection("connectionStrings"); if (section != null && section.SectionInformation.IsProtected) { section.SectionInformation.UnprotectSection(); config.Save(); } ConfigurationSection section1 = config.GetSection("appSettings"); if (section1 != null && section1.SectionInformation.IsProtected) { section1.SectionInformation.UnprotectSection(); config.Save(); } } }
获得当前起作用的配置文件
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile