c# 获取app.config中的值
来源:互联网 发布:小说改编的悬疑网络剧 编辑:程序博客网 时间:2024/06/08 14:26
/// 读操作
/// </summary>
/// <param name="appKey"></param>
/// <returns></returns>
public string GetConfigValue(string appKey)
{
XmlDocument xDoc = new XmlDocument();
try
{
xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
XmlNode xNode;
XmlElement xElem;
xNode = xDoc.SelectSingleNode("//appSettings");
xElem = (XmlElement)xNode.SelectSingleNode("//add[@key='" + appKey + "']");
if (xElem != null)
return xElem.GetAttribute("value");
else
return "";
}
catch (Exception)
{
return "";
}
}
///在config中设置想要的值
public static void SetValue(string AppKey, string AppValue)
{
XmlDocument xDoc = new XmlDocument();
//获取可执行文件的路径和名称
xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
XmlNode xNode;
XmlElement xElem1;
XmlElement xElem2;
xNode = xDoc.SelectSingleNode("//appSettings");
xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
if (xElem1 != null) xElem1.SetAttribute("value", AppValue);
else
{
xElem2 = xDoc.CreateElement("add");
xElem2.SetAttribute("key", AppKey);
xElem2.SetAttribute("value", AppValue);
xNode.AppendChild(xElem2);
}
xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");
}
///更新config的值
public static void UpdateConfig(string p_strKey, string p_strValue)
{
try
{
string m_strFullPath = "";
// Assembly Asm = Assembly.GetExecutingAssembly();
XmlDocument xmlDoc = new XmlDocument();
m_strFullPath = System.Windows.Forms.Application.ExecutablePath + ".config";
xmlDoc.Load(m_strFullPath);
XmlNodeList nodeList = xmlDoc.SelectSingleNode("/configuration/appSettings").ChildNodes;
foreach (XmlNode xn in nodeList)
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("key").IndexOf(p_strKey) != -1)
{
xe.SetAttribute("value", p_strValue);
}
}
xmlDoc.Save(m_strFullPath);
}
catch (System.NullReferenceException NullEx)
{
throw NullEx;
}
catch (Exception ex)
{
throw ex;
}
}
用时候ConfigurationManager.AppSettings["ImageCacheLocation"]获取config的值获取不到可以使用GetConfigValue();
转自:http://blog.csdn.net/xiaoweiserver/article/details/16342595
- c# 获取app.config中的值
- C# 获取app.config中的信息
- 在DLL执行中获取App.config中的值
- C#读写app.config中的数据
- C#读写app.config中的数据
- C#读写app.config中的数据
- C#读写app.config中的数据(收藏)
- C#读写app.config中的数据
- C#读写app.config中的数据
- C#读写app.config中的数据
- C#实现读写app.config中的connectionStrings
- C#读写app.config中的数据
- C#读写app.config中的数据
- C#读写app.config中的数据
- 【引用】C#读写app.config中的数据
- C#读写app.config中的数据
- C# 中的 App.config 文件配置
- C# aspx获取web.config中的key值
- IntelliJ IDEA 显示行号方法
- 任意长度字符串转化为16位定长的数(类似加密)
- Python|计蒜客——最后一个单词的长度
- 静态持续性、外部存储性
- Spring、Mybatis注解事物运用
- c# 获取app.config中的值
- MySQL的权限
- .net基础--(1)
- 如何重新编译jni
- HTML——信息提示(placeholder的简介与用法)
- 初见Hadoop—- 搭建MyEclipse 访问HDFS 上的文件
- androidstudio之增加删除library库文件
- BZOJ2818 Gcd[莫比乌斯反演]
- linux中log文件中各个文件对应的内容