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