zoukankan      html  css  js  c++  java
  • c# 配置文件的读写

    一、配置文件 app.config

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <appSettings>
            <add key="String1" value="value1" />
            <add key="String2" value="value2+" />
            <add key="String1" value="value3" />        
        </appSettings>
        <startup>
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
        </startup>
    </configuration>

    使用<appSettings></appSettings>标签增加节点,

    使用<add key="String1" value="value1" />增加键值对

    <startup>

       <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>

    </startup>

    设置.NET框架
    二、配置文件读写函数
     /// <summary>
     /// 读数配置参数值
     /// </summary>
     /// <param name="key">参数名称</param>
     /// <returns>参数的值</returns>
     public static string ReadConfig(string key)
    {
       List<string> list = new List<string>();
       ExeConfigurationFileMap file = new ExeConfigurationFileMap();
       file.ExeConfigFilename = System.Windows.Forms.Application.ExecutablePath + ".config";
       Configuration config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
       var myApp = (AppSettingsSection)config.GetSection("appSettings");
       return myApp.Settings[key].Value;
    
    }
    /// <summary>
    /// 写改配置参数中指定参数的值
    /// </summary>
    /// <param name="key">参数名称</param>
    /// <param name="value">参数修改的值</param>
    public static void WriteConfig(string key, string value)
    {
       ExeConfigurationFileMap file = new ExeConfigurationFileMap();
       file.ExeConfigFilename = System.Windows.Forms.Application.ExecutablePath + ".config";
       Configuration config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
       var myApp = (AppSettingsSection)config.GetSection("appSettings");
       myApp.Settings[key].Value = value;
       config.Save();
    }
     
    
    
  • 相关阅读:
    零基础入门:实时音视频技术基础知识全面盘点
    IM开发干货分享:如何优雅的实现大量离线消息的可靠投递
    flask_apscheduler 定时任务框架
    小程序使用 editor 富文本编辑器填坑指南
    万年深坑:mpVue 中的坑
    js利用canvas绘制爱心
    【字节跳动21届提前批】面试手撕代码——电梯调度算法
    Maximum call stack size exceeded 报错
    未安装less-loder
    05 vue前端工程化
  • 原文地址:https://www.cnblogs.com/braceli/p/5432880.html
Copyright © 2011-2022 走看看