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,保存的配置文件位置

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM