zoukankan      html  css  js  c++  java
  • C#使用json配置文件方法【读写Json,适合小项目】

    1,DAL中添加帮助类JsonConfigHelper

    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    /*
     * 需要添加引用Newtonsoft.Json.dl
     */
    
    namespace KobelcoReportDAL
    {
      public class JsonConfigHelper
        {
    
            private static Dictionary<string, string> configDic = new Dictionary<string, string>();
    
            /// <summary>
            /// 读取配置信息
            /// </summary>
            /// <param name="key"></param>
            /// <returns></returns>
            public static string ReadConfig(string key)
            {
                if (File.Exists("config.json") == false)//如果不存在就创建file文件夹
                {
                    FileStream f = File.Create("config.json");
                    f.Close();
                }
                string s = File.ReadAllText("config.json");
                try
                {
                    configDic = JsonConvert.DeserializeObject<Dictionary<string, string>>(s);
                }
                catch
                {
                    configDic = new Dictionary<string, string>();
                }
    
                if (configDic != null && configDic.ContainsKey(key))
                {
                    return configDic[key];
                }
                else
                {
                    return string.Empty;
                }
            }
    
            /// <summary>
            /// 添加配置信息
            /// </summary>
            /// <param name="key"></param>
            /// <param name="value"></param>
            public static void WriteConfig(string key, string value)
            {
                if (configDic == null)
                {
                    configDic = new Dictionary<string, string>();
                }
                configDic[key] = value;
                string s = JsonConvert.SerializeObject(configDic);
                File.WriteAllText("config.json", s);
            }
    
            /// <summary>
            /// 删除配置信息
            /// </summary>
            /// <param name="key"></param>
            public static void DeleteConfig(string key)
            {
                if (configDic != null && configDic.ContainsKey(key))
                {
                    configDic.Remove(key);
                    string s = JsonConvert.SerializeObject(configDic);
                    File.WriteAllText("config.json", s);
                }
            }
        }
    }
    

      

    2,读取配置文件【窗体加载的时候】

                //【1】读取配置文件
                filePath.RefFilePath = JsonConfigHelper.ReadConfig(this.txt_ReferenceFilePath.Name);
                filePath.MatchFilePath= JsonConfigHelper.ReadConfig(this.txt_MatchingFilePath.Name);
                filePath.AtlasDataFilePath= JsonConfigHelper.ReadConfig(this.txt_AtlasFilePath.Name);
                //【2】为控件赋值
                this.txt_ReferenceFilePath.Text= filePath.RefFilePath;
                this.txt_MatchingFilePath.Text = filePath.MatchFilePath;
                this.txt_AtlasFilePath.Text = filePath.AtlasDataFilePath;
    

      

    3,修改配置文件【修改保存按钮】

            //修改配置文件
            private void btn_modify_Click(object sender, EventArgs e)
            {
                JsonConfigHelper.WriteConfig(this.txt_ReferenceFilePath.Name, this.txt_ReferenceFilePath.Text);
                JsonConfigHelper.WriteConfig(this.txt_MatchingFilePath.Name, this.txt_MatchingFilePath.Text);
                JsonConfigHelper.WriteConfig(this.txt_AtlasFilePath.Name, this.txt_AtlasFilePath.Text);
            }
    

     

    4,保存的配置文件位置

     

  • 相关阅读:
    [NOI Online 提高组]序列
    微积分(下)
    微积分(上)
    [FJOI2018]领导集团问题
    [HNOI2015]亚瑟王
    [THUWC2017]随机二分图
    【模板】K级祖先(长链剖分)
    [CF438E]The Child and Binary Tree
    [洛谷P4841][集训队作业2013]城市规划
    [洛谷P4389]付公主的背包
  • 原文地址:https://www.cnblogs.com/baozi789654/p/15645897.html
Copyright © 2011-2022 走看看