How to change the app. config configuration file

Source: Internet
Author: User

  

1. Modify appsettings settings

Configuration config = configurationmanager.openexeconfiguration (configurationuserlevel.none);
var seturl = config. appsettings.settings["Apiaddress"];
if (seturl==null)
{
Config. APPSETTINGS.SETTINGS.ADD ("Apiaddress", apihelper.apiurl);;
Config. Save (configurationsavemode.modified);//Saving
Configurationmanager.refreshsection ("appSettings");//refresh (to prevent read-in memory)
}
else if (seturl.value! = Apihelper.apiurl)
{
Config. appsettings.settings["Apiaddress"]. Value = Apihelper.apiurl;
Config. Save (configurationsavemode.modified);
Configurationmanager.refreshsection ("appSettings");
}

2. Modify the connection string for the current project according to the AppConfig of the other project

<summary>
Detects if the config for the API project is changed and updates the current project's config if changed
</summary>
<param name= "connectionStringName" ></param>
<param name= "Newconnectionstring" ></param>
private void Updateconfigdbaddress ()
{
Try
{
var licenseconfig = configurationmanager.openexeconfiguration (Configurationuserlevel.none);
String apipath = Path.Combine (AppDomain.CurrentDomain.BaseDirectory, "... \\Web.config ");
Execonfigurationfilemap map = new Execonfigurationfilemap ();
Map. Execonfigfilename = Apipath;
Configuration apiconfig = configurationmanager.openmappedexeconfiguration (map, Configurationuserlevel.none);

Updateconfigdbaddress (Licenseconfig, Apiconfig, "connectionStringName");
}
Catch
{ }
}

private void Updateconfigdbaddress (Configuration licenseconfig,configuration apiconfig,string connectionstringname)
{
Connectionstringssection apisection = (connectionstringssection) apiconfig.getsection ("connectionStrings");
if (apisection.connectionstrings[connectionstringname]! = NULL)
{
String newvalue = Apisection.connectionstrings[connectionstringname]. ToString ();
Connectionstringssection licensesection = (connectionstringssection) licenseconfig.getsection ("ConnectionStrings") ;
if (licensesection.connectionstrings[connectionstringname] = = null)
{
Connectionstringsettings connectionstringsettings = new Connectionstringsettings (connectionStringName, NewValue);
LICENSESECTION.CONNECTIONSTRINGS.ADD (connectionstringsettings);
Licenseconfig.save (Configurationsavemode.full);
}
else if (newvalue! = Licensesection.connectionstrings[connectionstringname]. ToString ())
{
Licensesection.connectionstrings[connectionstringname]. ConnectionString = newvalue;
Licenseconfig.save (Configurationsavemode.full);
}
}
}

How to change the app. config configuration file

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.