1.实例

            //1.简单获取内容
            string value = ConfigurationManager.AppSettings["one"] as string;
            Console.WriteLine(value);


            //获取Configuration 对象
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            //1.根据key读取 <add /> 元素的value,
            /*
             *  a.如果指定的key不存在抛出异常
             *  b.如果没有指定value 返回空字符串
             */
            string value2 = config.AppSettings.Settings["two"].Value;
            Console.WriteLine(value2 == "");

            /*********以下修改操作失败**************/
            //2.增加 <add /> 元素
            config.AppSettings.Settings.Add("one", "http://www.baidu.com");
            //3.删除 <add /> 元素
            //config.AppSettings.Settings.Remove("two");
            //一定要保存,写不带参数的Save()也可以
            config.Save(ConfigurationSaveMode.Modified);
            //config.Save(ConfigurationSaveMode.Full);
            //查看当前 配置文件的路径
            Console.WriteLine(config.FilePath);
            //刷新
            ConfigurationManager.RefreshSection("appSettings");

 

相关文章:

  • 2021-07-30
  • 2021-07-05
  • 2021-08-28
  • 2021-08-03
  • 2021-06-11
  • 2021-09-16
  • 2021-09-06
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-13
  • 2021-11-22
  • 2022-12-23
  • 2021-09-25
相关资源
相似解决方案