1. Introduction
Application configuration file() is a standard XML file, and XML tags and attributes are case sensitive. It can be changed as needed, and developers can use configuration files to change settings without having to recompile the application.
*.Configuration file style:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> </connectionStrings> <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> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration>
2. Code
1. Configuration file read and write class
using System; using ; using ; using ; using ; using ; using ; using ; namespace XMLDemo1 { public static class ConfigHelper { #region ConnectionStrings //Return the data connection string based on the connection string name connectionName public static string GetConnectionStringsConfig(string connectionName) { //Specify config file reading string file = ; config = (file); string connectionString = [connectionName].(); return connectionString; } ///<summary> ///Update the connection string ///</summary> ///<param name="newName">Connection string name</param> ///<param name="newConString">Connection string content</param> ///<param name="newProviderName">Data provider name</param> public static void UpdateConnectionStringsConfig(string newName, string newConString, string newProviderName) { //Specify config file reading string file = ; Configuration config = (file); bool exist = false; //Record whether the connection string already exists //If the connection string to be changed already exists if ([newName] != null) { exist = true; } // If the connection string already exists, delete it first if (exist) { (newName); } // Create a new connection string instance ConnectionStringSettings mySettings = new ConnectionStringSettings(newName, newConString, newProviderName); // Add a new connection string to the configuration file. (mySettings); // Save changes to the configuration file (); // Force reload the ConnectionStrings configuration section of the configuration file ("ConnectionStrings"); } #endregion #region appSettings ///<summary> ///Return the value item of the appSettings configuration section in the *. file ///</summary> ///<param name="strKey"></param> ///<returns></returns> public static string GetAppConfig(string strKey) { //D:\Winform\xml Document Operation\XMLDemo1\bin\Debug\ string file = ; Configuration config = (file); foreach (string key in ) { if (key == strKey) { return [strKey].(); } } return null; } ///<summary> ///Add a pair of key-value pairs in the appSettings configuration section in the *. file ///</summary> ///<param name="newKey"></param> ///<param name="newValue"></param> public static void UpdateAppConfig(string newKey, string newValue) { string file = ; Configuration config = (file); bool exist = false; foreach (string key in ) { if (key == newKey) { exist = true; } } if (exist) { (newKey); } (newKey, newValue); (); ("appSettings"); } #endregion #region // Modify the IP addresses of all service endpoints public static void UpdateServiceModelConfig(string configPath, string serverIP) { Configuration config = (configPath); ConfigurationSectionGroup sec = [""]; ServiceModelSectionGroup serviceModelSectionGroup = sec as ServiceModelSectionGroup; ClientSection clientSection = ; foreach (ChannelEndpointElement item in ) { string pattern = @"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b"; string address = (); string replacement = ("{0}", serverIP); address = (address, pattern, replacement); = new Uri(address); } (); (""); } // Modify the IP address of the service in applicationSettings public static void UpdateConfig(string configPath, string serverIP) { Configuration config = (configPath); ConfigurationSectionGroup sec = ["applicationSettings"]; ConfigurationSection configSection = [""]; ClientSettingsSection clientSettingsSection = configSection as ClientSettingsSection; if (clientSettingsSection != null) { SettingElement element1 = ("DataService_SystemManagerWS_SystemManagerWS"); if (element1 != null) { (element1); string oldValue = ; = GetNewIP(oldValue, serverIP); (element1); } SettingElement element2 = ("DataService_EquipManagerWS_EquipManagerWS"); if (element2 != null) { (element2); string oldValue = ; = GetNewIP(oldValue, serverIP); (element2); } } (); ("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 = ("{0}", serverIP); string newvalue = (oldValue, pattern, replacement); return newvalue; } #endregion } }
function
namespace XMLDemo1 { class Program { static void Main(string[] args) { try { //D:\Winform\xml Document Operation\XMLDemo1\bin\Debug\ //string file0 = ; //D:\Winform\xml Document Operation\XMLDemo1\bin\Debug\ //string file = + ".config"; //D:\Winform\xml Document Operation\XMLDemo1\bin\Debug\ //string file1 = ; string serverIP = ("ServerIP"); string db = ("DataBase"); string user = ("user"); string password = ("password"); (serverIP); (db); (user); (password); ("ServerIP", "192.168.0.1"); string newIP = ("ServerIP"); (newIP); ("connstr4", ".......", ""); (); } catch (Exception ex) { (); } } } }
This is all about this article about C# reading and writing Config configuration files. I hope it will be helpful to everyone's learning and I hope everyone will support me more.