zoukankan      html  css  js  c++  java
  • 读取、修改配置文件节点

    1、【引用程序集】

    using System.Configuration;

    2、【读取配置文件值】【更改配置文件值】

    public class AppSettingsUtil
        {
            /// <summary>
            /// 获取配置文件某一节点值
            /// </summary>
            /// <param name="key"></param>
            /// <returns></returns>
            public static string GetValueByKey(string key) {
                return ConfigurationManager.AppSettings[key];
            }
            /// <summary>
            /// 更改配置文件的值
            /// </summary>
            /// <param name="key"></param>
            /// <param name="value"></param>
            public static void UpdateByKey(string key, string value) {
                Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                configuration.AppSettings.Settings[key].Value = value;
                configuration.Save();
                ConfigurationManager.RefreshSection("appSettings");
            }
        }

    3、【配置文件中示例】

    <appSettings>
        <add key="blog" value="多1份努力~"/>
      </appSettings>

    4、【调用示例】

    static void Main(string[] args)
            {
                Console.WriteLine("---------- key等于【blog】的值 ----------");
                Console.WriteLine(AppSettingsUtil.GetValueByKey("blog"));
                Console.WriteLine("---------- 请输入需要保存的值 ----------");
                string s=Console.ReadLine();
                Console.WriteLine("---------- 修改之后key等于【blog】的值 ----------");
                AppSettingsUtil.UpdateByKey("blog",s);
                Console.WriteLine(AppSettingsUtil.GetValueByKey("blog"));
                Console.ReadLine();
            }

    查看配置文件中的值,如图所示,值已更改为“风落尘”【注意,需要查看的配置文件在程序生成的bin文件夹下面的debug文件夹下】

  • 相关阅读:
    网友心得 说说.NET中的反射(转帖)
    javascript的函数(转)
    asp.net基于窗体的身份验证
    创建ASP.NET WEB自定义控件(转)
    .net调用Oracle存储过程
    写字间里程序员
    世界四大杀毒软件调侃
    技巧/诀窍:在ASP.NET中重写URL(转)
    VS2008中JavaScript编辑调试器的秘密
    如何用C#语言构造蜘蛛程序
  • 原文地址:https://www.cnblogs.com/z-huan/p/7575838.html
Copyright © 2011-2022 走看看