• 在配置文件中定义初始值然后读取


    先添加名为ConfigUtil 的类文件

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Configuration;
    using System.Xml;

    namespace MakeSiteMap.Utils
    {
        /// <summary>
        /// 对配置文件[App.config]进行操作
        /// </summary>
        public class ConfigUtil
        {
            #region Methods
            /// <summary>
            /// 通过 key 读取配置文件的值
            /// </summary>
            /// <param name="key"><add key="" value ="" /></param>
            /// <returns></returns>
            public static string Read(string key)
            {        
                return ConfigurationManager.AppSettings[key];
            }

            /// <summary>
            /// 保存修改后的配置文件
            /// </summary>
            /// <param name="key">键</param>
            /// <param name="value">值</param>
            public static void SaveConfig(string key, string value)
            {
                Configuration cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                cfg.AppSettings.Settings[key].Value = value;
                cfg.Save(ConfigurationSaveMode.Modified);
            }

            /// <summary>
            /// 保存修改后的配置文件[XML方式]
            /// </summary>
            /// <param name="key">键</param>
            /// <param name="value">值</param>
            public static void Save(string key, string value)
            {
                XmlDocument doc = new XmlDocument();

                string config = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

                doc.Load(config);

                XmlNodeList nodes = doc.GetElementsByTagName("add");
                for (int i = 0; i < nodes.Count; i++)
                {
                    XmlAttribute att = nodes[i].Attributes["key"];

                    if (att.Value == key)
                    {
                        att = nodes[i].Attributes["value"];
                        att.Value = value;
                        break;
                    }
                }

                doc.Save(config);
            }
            #endregion
        }
    }

    =========================================================

    然后再配置文件中设定初始值在</appSettings>之上

       <add key="CurrentNum" value="77818" />
      </appSettings>

    然后在程序中读取CurrentNum的值value

    string file = ConfigUtil.Read("CurrentNum");

    再在程序一次执行完成后修改配置文件中CurrentNum

    file = ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1]["商家编码"].ToString();  //修改
                    ConfigUtil.SaveConfig("CurrentNum", file);   //保存修改

  • 相关阅读:
    RESTful规范
    浏览器缓存(强缓存和协商缓存)
    react hooks useState更新数据不及时问题及处理
    css提升页面渲染新属性content-visibility
    Mongodb Sort Operations
    mongodb使用小点
    Visual Studio 2017 中的Git源代码控制中使用BeyondCompare 3
    “指纹登录“ -- 项目中用到的两个cordova插件基本使用
    代码段:通过索引获取对应的Excel列名; 索引从0开始,返回形如 A,B,C,...,Z,AA,AB,...,AZ,BA,...,ZZ,AAA,AAB,......
    初学knockoutjs记录9——Bindings 绑定(1 Controling text and appearance 控制文本和外观)
  • 原文地址:https://www.cnblogs.com/happygx/p/1957993.html
Copyright © 2020-2023  润新知