Public classappmodify {/// <summary> ///return data connection string based on connection string name ConnectionName/// </summary> /// <param name= "ConnectionName" ></param> /// <returns></returns> Public Static stringGetconnectionstringsconfig (stringconnectionname) { stringConnectionString =Configurationmanager.connectionstrings[connectionname]. Connectionstring.tostring (); returnconnectionString; } /// <summary> ///Update Connection string/// </summary> /// <param name= "NewName" >Connection string Name</param> /// <param name= "newconstring" >Connection String Contents</param> /// <param name= "Newprovidername" >Data Provider Name</param> Public Static voidUpdateconnectionstringsconfig (stringNewName,stringnewconstring,stringnewprovidername) { BOOLIsModified =false;//record whether the connection string already exists//If the connection string you want to change already exists if(Configurationmanager.connectionstrings[newname]! =NULL) {ismodified=true; } //Create a new connection string instanceConnectionstringsettings mysettings =Newconnectionstringsettings (NewName, newconstring, newprovidername); //Open the executable configuration file *.exe.configConfiguration config =configurationmanager.openexeconfiguration (Configurationuserlevel.none); //if the connection string already exists, remove it first if(ismodified) {config. ConnectionStrings.ConnectionStrings.Remove (NewName); } //adds a new connection string to the configuration file.CONFIG. CONNECTIONSTRINGS.CONNECTIONSTRINGS.ADD (mysettings); //save changes made to the configuration fileCONFIG. Save (configurationsavemode.modified); //forcing the connectionstrings configuration section of the configuration file to be re-loadedConfigurationmanager.refreshsection ("ConnectionStrings"); } Public Static voidUpdateconnectionstringsconfig (stringNewName,stringnewconstring) { BOOLIsModified =false;//record whether the connection string already exists//If the connection string you want to change already exists if(Configurationmanager.connectionstrings[newname]! =NULL) {ismodified=true; } //Create a new connection string instanceConnectionstringsettings mysettings =Newconnectionstringsettings (NewName, newconstring); //Open the executable configuration file *.exe.configConfiguration config =configurationmanager.openexeconfiguration (Configurationuserlevel.none); //if the connection string already exists, remove it first if(ismodified) {config. ConnectionStrings.ConnectionStrings.Remove (NewName); } //adds a new connection string to the configuration file.CONFIG. CONNECTIONSTRINGS.CONNECTIONSTRINGS.ADD (mysettings); //save changes made to the configuration fileCONFIG. Save (configurationsavemode.modified); //forcing the connectionstrings configuration section of the configuration file to be re-loadedConfigurationmanager.refreshsection ("ConnectionStrings"); } /// <summary> ///returns the value entry for the appsettings configuration section in the *.exe.config file/// </summary> /// <param name= "strkey" ></param> /// <returns></returns> Public Static stringGetappconfig (stringstrkey) { foreach(stringKeyinchconfigurationmanager.appsettings) {if(Key = =strkey) { returnConfigurationmanager.appsettings[strkey]; } } return NULL; } /// <summary> ///add a pair of key, value pairs to the appsettings configuration section in the *.exe.config file/// </summary> /// <param name= "NewKey" ></param> /// <param name= "NewValue" ></param> Public Static voidUpdateappconfig (stringNewKey,stringnewvalue) { BOOLIsModified =false; foreach(stringKeyinchconfigurationmanager.appsettings) {if(Key = =NewKey) {ismodified=true; } } //Open App. Config of executableConfiguration config =configurationmanager.openexeconfiguration (Configurationuserlevel.none); //You need to remove the old settings object before you can replace it if(ismodified) {config. AppSettings.Settings.Remove (NewKey); } //Add an application Setting.CONFIG. APPSETTINGS.SETTINGS.ADD (NewKey, newvalue); //Save The changes in app. Config file.CONFIG. Save (configurationsavemode.modified); //Force A reload of a changed section.Configurationmanager.refreshsection ("appSettings"); } }
Appmodify Modifying app. Config