Change the value in app. Config and save

Source: Internet
Author: User

Using System;
Using System.Collections.Generic;
Using System.Configuration;
Using System.IO;
Using System.Linq;
Using System.Reflection;
Using System.Text;
Using System.Threading.Tasks;
Using System.Xml;


Namespace Handpickcrawlerdb.extensions
{
public class Apphelper
{
private static string _appconfig = null;


public static string AppConfig
{
Get
{
if (_appconfig = = null)
{
Type t = typeof (System.Configuration.ConfigurationManager). Assembly.GetType ("System.Configuration.ClientConfigurationHost");
Object cfghst = Activator.CreateInstance (t, true);
PropertyInfo pi = t.getproperty ("configpaths", BindingFlags.Instance | BindingFlags.NonPublic);
Object Cfgpath = Pi. GetValue (cfghst, NULL);


Type T1 = typeof (System.Configuration.ConfigurationManager). Assembly.GetType ("System.Configuration.ClientConfigPaths");
PI = t1. GetProperty ("Applicationconfiguri", BindingFlags.Instance | BindingFlags.NonPublic);
String path = (string) pi. GetValue (Cfgpath, NULL);


if (string. IsNullOrEmpty (PATH))
_appconfig = string. Empty;
Else
_appconfig = path. Replace (". Vshost.", ".");
}
return _appconfig;
}
Set
{
_appconfig = value;
}
}


public static void Setsettingtoappconfig (string key, String value)
{
if (string. IsNullOrEmpty (Key))
{
throw new Exception ("key not being null");
}
Else
{
Key = key. Trim ();
}
if (string. IsNullOrEmpty (value))
Value = "";
Else
Value = value. Trim ();


if (! File.exists (AppConfig))
{
throw new DirectoryNotFoundException ();
}
File.setattributes (AppConfig, fileattributes.normal);
XmlDocument xmldoc = new XmlDocument ();
XmlDoc. Load (AppConfig);
XmlNodeList Xmllst = xmldoc. SelectNodes ("/configuration/appsettings/add");
if (xmldoc. selectSingleNode ("/configuration/appsettings") = = null)
{
XmlNode n2 = xmldoc. CreateNode ("element", "appSettings", "");
N2. InnerXml = "<add key=\" "+ key +" \ "value=\" "+ Value +" \ "/>";
XmlDoc. selectSingleNode ("/configuration"). AppendChild (N2);
XmlDoc. Save (AppConfig);
}
else if (xmllst. Count = = 0)
{
XmlNode n2 = xmldoc. CreateNode ("element", "Add", "");
XmlAttribute xa = xmldoc. CreateAttribute ("key");
Xa. Value = key;
N2. Attributes.append (XA);
Xa = xmldoc. CreateAttribute ("value");
Xa. Value = value;
N2. Attributes.append (XA);
XmlDoc. selectSingleNode ("/configuration/appsettings"). AppendChild (N2);
XmlDoc. Save (AppConfig);
}
Else
{
bool existed = false;
foreach (XmlNode n1 in Xmllst)
{
if (N1. attributes["Key"]. Value.toupper () = = key. ToUpper ())
{
N1. attributes["Value"]. Value = value;
XmlDoc. Save (AppConfig);
existed = true;
Break
}
}
if (!existed)
{
XmlNode xmlnd = xmldoc. selectSingleNode ("/configuration/appsettings");
XmlNode n2 = xmldoc. CreateNode ("element", "Add", "");
XmlAttribute xa = xmldoc. CreateAttribute ("key");
Xa. Value = key;
N2. Attributes.append (XA);
Xa = xmldoc. CreateAttribute ("value");
Xa. Value = value;
N2. Attributes.append (XA);
Xmlnd. AppendChild (N2);
XmlDoc. Save (AppConfig);
}
}
Configurationmanager.refreshsection ("appSettings");
}
}
}

Change the value in app. Config and save

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.