Appmodify Modifying app. Config

Source: Internet
Author: User

 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.