码迷,mamicode.com
首页 > 其他好文 > 详细

C#配置文件中怎么通过key来获取value值

时间:2014-09-28 00:13:30      阅读:497      评论:0      收藏:0      [点我收藏+]

标签:des   io   ar   for   文件   sp   art   on   c   

public static string AppConfig()
{
  return System.IO.Path.Combine(Application.StartupPath, "BlackBoxForms.exe.config");
}

 

/// <summary>
/// 根据Key获取Value值
/// </summary>
/// <param name="appKey">Key</param>
/// <returns>Value值</returns>
public static string GetValueByKey(string appKey)
{
  try
  {
    string xmlValue = "";
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(AppConfig());
    XmlNode xNode = xmlDoc.SelectSingleNode("//appSettings");
    foreach (XmlNode node in xNode.ChildNodes)
    {
    if (node.Attributes["key"].Value == appKey)
    {
      xmlValue = node.Attributes["value"].Value;
    }
  }
    return xmlValue;
  }
  catch (Exception ex)
  {
    return ex.Message;
  }
}

C#配置文件中怎么通过key来获取value值

标签:des   io   ar   for   文件   sp   art   on   c   

原文地址:http://www.cnblogs.com/liuhuanping/p/3997415.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!