我在项目里添加一个App.config配置文件,里面的代码如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="connectionstring" value="User ID=sa;Data Source=.;
  Password=;Initial Catalog=test;Provider=SQLOLEDB.1;" />
    <add key="TemplatePATH" value="Template" />
  </appSettings>
</configuration>
我现在在窗体 Form1里面 如何去读取这个配置文件的属性呢,
我已经引用命名空间using System.Collections;
就打个比方去读取这个配置文件value的属性吧,谢谢.

解决方案 »

  1.   

    利用XML读取方法,百度一下就可以了,很简单的
      

  2.   

    ConfigurationManager.AppSettings["keyname"]
      

  3.   

    ConfigurationManager.AppSettings[“KEY”]
      

  4.   

    string settingValue = System.Configuration.ConfigurationManager.AppSettings.Get("Key");
      

  5.   

    为什么 代码都跟 App.config 无关呢, 难道代码自动去找到这个文件吗
      

  6.   

    ConfigurationManager 是引用那个命名空间呀,好像我引用的命名空间不够.
      

  7.   

    using System.Configuration;这个config是VS自己给你加的,build后会变成你的程序名字.exe.config文件,你不用理。
      

  8.   

    我是
    private void button1_Click(object sender, EventArgs e)
            {            
                string ss = ConfigurationManager.AppSettings["keyname"];
                MessageBox.Show(ss);
      
            }
    这样子写的
    结果呢 报如下错误
    错误 1 当前上下文中不存在名称“ConfigurationManager” D:\测试\加app配置文件\加app配置文件\Form1.cs 72 25 加app配置文件
      

  9.   

    System.Configuration.ConfigurationManager.AppSettings["keyname"]需要添加Reference
    否则就用
    ConfigurationSettings.AppSettings[“keyname”];
    看来你用的是.net3.5+
      

  10.   

    先在引用里添加 System.Configuration, 然后在程序里导入命名空间。获取和修改配置文件方法:
     public static bool RegEdit(Hashtable Ht)
            {
                if (Ht.Count > 0)
                {
                    Configuration C = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    foreach (DictionaryEntry s in Ht)
                    {
                        C.AppSettings.Settings[s.Key.ToString()].Value = s.Value.ToString();
                    }
                    C.Save();
                }
                return true;        }
            public static string GetReg(string Key)
            {
                Configuration C = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                return C.AppSettings.Settings[Key].Value;
            }
      

  11.   

    public static bool RegEdit(Hashtable Ht)
            {
    其中的Hashtable  是什么呢
      

  12.   

    http://hi.baidu.com/zkbob22/blog/item/1da0b8dbfe6082d6b7fd487b.htmlC#使用配置文件(.settings、.config)存储应用程序配置