想弄一个函数从配置文件里读取值
App.config
<?xml version="1.0" encoding="utf-8" ?><configuration>
  
  <!--实时数据存储系统初始化需要的参数-->  <appSettings>        <add key="pointCount" value="7"/>    <add key="maxOffset" value="0.005"/>    <add key="timerInterval" value="5000"/>    <add key="bufferLength" value="20"/>    <add key="readInterval" value="1500"/>    <add key="storePath" value="c:\\"/>  </appSettings></configuration>
函数
 private static string GetAppConfig(string strKey)
        {
            foreach (string key in ConfigurationManager.AppSettings)
            {
                if (key == strKey)
                {                    return ConfigurationManager.AppSettings[strKey];                }
                            }
                        return null;
           
        }
怎么弄也不成功,各位大牛帮我看看啊

解决方案 »

  1.   

    直接返回ConfigurationManager.AppSettings[strKey];
      

  2.   

    XmlDocument doc = new XmlDocument();
    doc.Load(filename); 
    XmlNode node = doc.SelectSingleNode("//appSettings"); 
    XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='" + appKey + "']");if (element != null)
    {
       value = element.GetAttribute("value");
    }System.Configuration.ConfigurationManager.AppSettings[""]);
      

  3.   

    你的项目是winform的吗?如果是,就只能按3楼的方法,否则那个配置文件的节点并不能通过强类型化访问,在WEB中才像你那样子的.如果你要简单些,就不要直接写那个配置文件,而应该用应用程序设置,因为你写的节点<appSettings> 默认是不存在的,WEB的配置才有