As shown below:
<?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>
Read-write class for Config profile
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 {//Based on connection string name ConnectionName return data connection string public static S Tring Getconnectionstringsconfig (String connectionname) {//Specify config file read string file = System.Windows.Form
S.application.executablepath;
System.Configuration.Configuration config = configurationmanager.openexeconfiguration (file); string connectionString = config. Connectionstrings.connectionstrings[connectionname].
Connectionstring.tostring ();
return connectionString; ///<summary>///Update connection string///</summary>///<param name= "NewName" > Connection string name </PARAM&G
T <param name= "newconstring" > Connection string content </param>///<param name= "Newprovidername" > Data provider name ≪/param> public static void Updateconnectionstringsconfig (String newName, String newconstring, String Newprovider
Name) {///Specifies that config file reads string file = System.Windows.Forms.Application.ExecutablePath;
Configuration config = configurationmanager.openexeconfiguration (file); BOOL exist = FALSE; Record if the connection string already exists///If the connection string you want to change already exists if (config).
Connectionstrings.connectionstrings[newname]!= null) {exist = true; //If the connection string already exists, first delete it if (exist) {config.
ConnectionStrings.ConnectionStrings.Remove (NewName); //Create a new connection string instance connectionstringsettings mysettings = new Connectionstringsettings (NewName, Newconstr
ING, newprovidername);
Adds a new connection string to the configuration file. Config.
CONNECTIONSTRINGS.CONNECTIONSTRINGS.ADD (mysettings); Saves the change config to the configuration file.
Save (configurationsavemode.modified); Force reload configuration file connectionstrings configuration section configurationmanager.refreshsection ("ConNectionstrings "); ///<summary>///Returns the value entry for the appsettings configuration section in *.exe.config file///</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>///appsettings configuration section in *.exe.config file adds a pair of key values to///</summary>///<param name= "n Ewkey "></param>///<param name=" newvalue "></param> public static void Updateappconfig (Strin
G 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"); //Modify the IP address of all service endpoints under System.ServiceModel the public static void Updateservicemodelconfig (String configpath, String ser
Verip) {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"); //Modify IP addresses for services in App.Properties.Settings in applicationsettings public static void Updateconfig (string configpath, str
ing 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; }
}
}
The
Test code is as follows:
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 (); The catch (Exception ex) {Console.WriteLine (ex).
message); }
}
}
Above this C # read and write config file is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.