首页 > 代码库 > 更改App.config里的值并保存
更改App.config里的值并保存
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 be null");
}
else
{
key = key.Trim();
}
if (string.IsNullOrEmpty(value))
value = http://www.mamicode.com/"";
else
value = http://www.mamicode.com/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=http://www.mamicode.com/"" + 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 = http://www.mamicode.com/key;
n2.Attributes.Append(xa);
xa = xmldoc.CreateAttribute("value");
xa.Value = http://www.mamicode.com/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 = http://www.mamicode.com/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 = http://www.mamicode.com/key;
n2.Attributes.Append(xa);
xa = xmldoc.CreateAttribute("value");
xa.Value = http://www.mamicode.com/value;
n2.Attributes.Append(xa);
xmlnd.AppendChild(n2);
xmldoc.Save(AppConfig);
}
}
ConfigurationManager.RefreshSection("appSettings");
}
}
}
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 be null");
}
else
{
key = key.Trim();
}
if (string.IsNullOrEmpty(value))
value = http://www.mamicode.com/"";
else
value = http://www.mamicode.com/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=http://www.mamicode.com/"" + 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 = http://www.mamicode.com/key;
n2.Attributes.Append(xa);
xa = xmldoc.CreateAttribute("value");
xa.Value = http://www.mamicode.com/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 = http://www.mamicode.com/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 = http://www.mamicode.com/key;
n2.Attributes.Append(xa);
xa = xmldoc.CreateAttribute("value");
xa.Value = http://www.mamicode.com/value;
n2.Attributes.Append(xa);
xmlnd.AppendChild(n2);
xmldoc.Save(AppConfig);
}
}
ConfigurationManager.RefreshSection("appSettings");
}
}
}
更改App.config里的值并保存
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。