Ini配置文件讀寫,增加備注功能

1.增加備注項寫入

例:

#節點備注
[A]
#項備注
bbb=1
ccc=2

[B]
bbb=1

?IniConfig2 ic = new IniConfig2();

//首次寫入

? ? ? ? ? ? if (!ic.CanRead())
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ic.AddSectionReMarke("A", "節點備注");
? ? ? ? ? ? ? ? ic.SetValue("A", "bbb", "1" );
? ? ? ? ? ? ? ? ic.SetValue("A", "ccc", "2");
? ? ? ? ? ? ? ? ic.SetValue("A", "ccc", "2");
? ? ? ? ? ? ? ? ic.SetValue("B", "bbb", "1");
? ? ? ? ? ? ? ? ic.AddItemReMarke("A","bbb", "項備注");
? ? ? ? ? ? }

//獲取值

? ? ? ? string a = ic.GetValue("A", "bbb");

    /// <summary>/// 配置文件讀寫類,支持中文和注釋保留/// 增加備注/// </summary>public class IniConfig2{//小結private class Section{public Section(){items = new List<ValueItem>();reMark = new List<string>();}public string name;//名稱public List<ValueItem> items;//子項public List<string> reMark;//備注}//項目private class ValueItem{public ValueItem(){reMark = new List<string>();}public string key;//鍵public string value;//值public List<string> reMark;//備注}private const string DefaultFileName = "Config.ini";//默認文件private readonly string filePath;public IniConfig2() : this(DefaultFileName){}public IniConfig2(string _fileName){if (string.IsNullOrWhiteSpace(_fileName)){_fileName = DefaultFileName;}_fileName = _fileName.EndsWith(".ini", StringComparison.OrdinalIgnoreCase) ? _fileName : $"{_fileName}.ini";filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, _fileName);Load();}public bool CanRead(){return File.Exists(filePath);}List<Section> lst_Section;private void Load(){lst_Section = new List<Section>();List<string> fileComments = new List<string>();fileComments.Clear();if (!File.Exists(filePath)){return;}using (StreamReader reader = new StreamReader(filePath, Encoding.UTF8)){string _txt, _preSectio = "", _preKey = "";List<string> lst_reMarke = new List<string>();Section sec = null;while ((_txt = reader.ReadLine()) != null){_txt = _txt.Trim();if (_txt == ""){continue;}if (_txt.StartsWith("#") || _txt.StartsWith(";")){// 收集注釋行lst_reMarke.Add(_txt);}else if (_txt.StartsWith("[") && _txt.EndsWith("]")){if (sec != null){lst_Section.Add(sec);}sec = new Section();//節名稱_preSectio = _txt.Substring(1, _txt.Length - 2).Trim();sec.name = _preSectio;// 保存小結前的注釋if (lst_reMarke.Count > 0){sec.reMark = lst_reMarke.ToList();lst_reMarke.Clear();}}else{// 鍵=值int index = _txt.IndexOf('=');if (index > 0 && _preSectio != ""){string key = _txt.Substring(0, index).Trim();string value = _txt.Substring(index + 1).Trim();_preKey = key;ValueItem content = new ValueItem();content.key = _preKey;content.value = value;sec.items.Add(content);}else{ValueItem ct = sec.items.FirstOrDefault(p => p.key == _preKey);if (ct != null){ct.value += Environment.NewLine + _txt;}}// 保存項注釋if (lst_reMarke.Count > 0){ValueItem ct = sec.items.FirstOrDefault(p => p.key == _preKey);if (ct != null){ct.reMark = lst_reMarke.ToList();lst_reMarke.Clear();}}}}//數據讀取完后,追加到lstif (sec != null){lst_Section.Add(sec);}}}public string GetValue(string section, string key){ValueItem ct = lst_Section.FirstOrDefault(p => p.name == section)?.items.FirstOrDefault(p => p.key == key);if (ct != null){return ct.value.Replace(Environment.NewLine, "");}else{return "";}}public void SetValue(string section, string key, string value){Section nt = lst_Section.FirstOrDefault(p => p.name == section);if (nt != null){ValueItem ct = nt.items.FirstOrDefault(p => p.key == key);if (ct != null){ct.value = value;}else{ct = new ValueItem();ct.key = key;ct.value = value;nt.items.Add(ct);}}else{nt = new Section();nt.name = section;ValueItem ct = new ValueItem();ct.key = key;ct.value = value;nt.items.Add(ct);lst_Section.Add(nt);}Save();}public void AddSectionReMarke(string section, string remarke){Section nt = lst_Section.FirstOrDefault(p => p.name == section);if (nt != null){nt.reMark.Add(remarke.StartsWith("#") ? remarke : "#" + remarke);}else{nt = new Section();nt.name = section;nt.reMark.Add(remarke.StartsWith("#") ? remarke : "#" + remarke);lst_Section.Add(nt);}Save();}public void AddItemReMarke(string section, string key, string remarke){Section nt = lst_Section.FirstOrDefault(p => p.name == section);if (nt != null){ValueItem ct = nt.items.FirstOrDefault(p => p.key == key);if (ct != null){ct.reMark.Add(remarke.StartsWith("#") ? remarke : "#" + remarke);}else{ct = new ValueItem();ct.key = key;ct.reMark.Add(remarke.StartsWith("#") ? remarke : "#" + remarke);lst_Section.Add(nt);}}else{nt = new Section();nt.name = section;ValueItem ct = new ValueItem();ct.key = key;ct.reMark.Add(remarke.StartsWith("#") ? remarke : "#" + remarke);lst_Section.Add(nt);}Save();}private void Save(){using (StreamWriter writer = new StreamWriter(filePath, false, Encoding.UTF8)){// 寫入各個sectionforeach (Section section in lst_Section){// 寫入section前的注釋foreach (string comment in section.reMark){writer.WriteLine(comment);}// 寫入sectionwriter.WriteLine($"[{section.name}]");// 寫入鍵值對foreach (ValueItem cnt in section.items){//寫備注foreach (string comment in cnt.reMark){writer.WriteLine(comment);}//寫值writer.WriteLine($"{cnt.key}={cnt.value}");}writer.WriteLine(); // 空行分隔不同的節}}}}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/82573.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/82573.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/82573.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

OpenHarmony 5.0中狀態欄添加以太網狀態欄圖標以及功能實現

目錄 1.前置條件 2.方案 1.前置條件 首先以太網接口是有問題的,如下按照如下流程將以太網接口進行修復 OpenHarmony 以太網卡熱插拔事件接口無效-CSDN博客 然后上述的接口可以了就可以通過這個接口獲取以太網是否連接狀態 要注意wifi連接的干擾和預置虛擬網口干擾 2.方案…

RNN GRU LSTM 模型理解

一、RNN 1. 在RNN中&#xff0c; 2. RNN是一個序列模型&#xff0c;與非序列模型不同&#xff0c;序列中的元素互相影響&#xff1a; 是由 計算得來的。 在前向傳播中&#xff1a; 用于計算 和 用于計算 和 因此&#xff0c;當進行反向鏈式法則求導時候&#xf…

多路徑傳輸(比如 MPTCP)控制實時突發

實時突發很難控制&#xff0c;因為 “實時” 和 “突發” 相互斥。實時要求避免排隊&#xff0c;而突發必然要排隊&#xff0c;最終的解決方案都指向找一個公說公有理&#xff0c;婆說婆有理的中間點&#xff0c;這并沒解決問題&#xff0c;只是權衡了問題。 這種局部解決問題的…

函數式編程思想詳解

函數式編程思想詳解 1. 核心概念 不可變數據 (Immutable Data) 數據一旦創建&#xff0c;不可修改。任何操作均生成新數據&#xff0c;而非修改原數據。 優點&#xff1a;避免副作用&#xff0c;提升并發安全&#xff0c;簡化調試。 Java實現&#xff1a;使用final字段、不可變…

iOS 主要版本發布歷史

截至 2025 年 5 月&#xff0c;iOS 的最新正式版本是 iOS 18&#xff0c;于 2024 年 9 月 16 日 正式發布。此前的 iOS 17 于 2023 年 9 月 18 日 發布&#xff0c;并在 2024 年被 iOS 18 取代。(維基百科) &#x1f4f1; iOS 主要版本發布歷史 以下是 iOS 各主要版本的發布日…

矩陣詳解:線性代數在AI大模型中的核心支柱

&#x1f9d1; 博主簡介&#xff1a;CSDN博客專家、CSDN平臺優質創作者&#xff0c;高級開發工程師&#xff0c;數學專業&#xff0c;10年以上C/C, C#, Java等多種編程語言開發經驗&#xff0c;擁有高級工程師證書&#xff1b;擅長C/C、C#等開發語言&#xff0c;熟悉Java常用開…

基于51單片機和8X8點陣屏、獨立按鍵的飛行躲閃類小游戲

目錄 系列文章目錄前言一、效果展示二、原理分析三、各模塊代碼1、8X8點陣屏2、獨立按鍵3、定時器04、定時器1 四、主函數總結 系列文章目錄 前言 用的是普中A2開發板。 【單片機】STC89C52RC 【頻率】12T11.0592MHz 【外設】8X8點陣屏、獨立按鍵 效果查看/操作演示&#xff…

區塊鏈可投會議CCF C--APSEC 2025 截止7.13 附錄用率

Conference&#xff1a;32nd Asia-Pacific Software Engineering Conference (APSEC 2025) CCF level&#xff1a;CCF C Categories&#xff1a;軟件工程/系統軟件/程序設計語言 Year&#xff1a;2025 Conference time&#xff1a;December 2-5, 2025 in Macao SAR, China …

pdf圖片導出(Visio\Origin\PPT)

一、Visio 導入pdf格式圖片 1. 設計->大小&#xff0c;適應繪圖。 2. 文件->導出&#xff0c;導出為pdf格式。 上面兩部即可得到只包含圖的部分的pdf格式。 如果出現的有默認白邊&#xff0c;可以通過以下方式設置&#xff1a; 1. 文件->選項->自定義功能區->…

vector的實現

介紹 1. 本質與存儲結構 動態數組實現&#xff1a;vector 本質是動態分配的數組&#xff0c;采用連續內存空間存儲元素&#xff0c;支持下標訪問&#xff08;如 vec[i]&#xff09;&#xff0c;訪問效率與普通數組一致&#xff08;時間復雜度 O (1)&#xff09;。動態擴容機制&…

【Linux筆記】防火墻firewall與相關實驗(iptables、firewall-cmd、firewalld)

一、概念 1、防火墻firewall Linux 防火墻用于控制進出系統的網絡流量&#xff0c;保護系統免受未授權訪問。常見的防火墻工具包括 iptables、nftables、UFW 和 firewalld。 防火墻類型 包過濾防火墻&#xff1a;基于網絡層&#xff08;IP、端口、協議&#xff09;過濾流量&a…

el-date-picker 前端時間范圍選擇器

控制臺參數&#xff1a; 前端代碼&#xff1a;用數組去接受&#xff0c;同時用 value-format"YYYY-MM-DD" 格式化值為&#xff1a;年月日格式 <!-- 查詢區域 --><transition name"fade"><div class"search" v-show"showSe…

在 macOS 上安裝 jenv 管理 JDK 版本

在 macOS 上安裝 jenv 并管理 JDK 版本 在開發 Java 應用程序時&#xff0c;你可能需要在不同的項目中使用不同版本的 JDK。手動切換 JDK 版本可能會很繁瑣&#xff0c;但幸運的是&#xff0c;有一個工具可以簡化這個過程&#xff1a;jenv。jenv 是一個流行的 Java 版本管理工…

2025年全國青少年信息素養大賽復賽C++集訓(16):吃糖果2(題目及解析)

2025年全國青少年信息素養大賽復賽C集訓&#xff08;16&#xff09;&#xff1a;吃糖果2&#xff08;題目及解析&#xff09; 題目描述 現有n(50 > n > 0)個糖果,每天只能吃2個或者3個&#xff0c;請計算共有多少種不同的吃法吃完糖果。 時間限制&#xff1a;1000 內存…

ARM筆記-嵌入式系統基礎

第一章 嵌入式系統基礎 1.1嵌入式系統簡介 1.1.1嵌入式系統定義 嵌入式系統定義&#xff1a; 嵌入式系統是以應用為中心&#xff0c;以計算機技術為基礎&#xff0c;軟硬件可剪裁&#xff0c;對功能、可靠性、成本、體積、功耗等有嚴格要求的專用計算機系統 ------Any devic…

大語言模型(LLM)入門項目推薦

推薦大語言模型(LLM)的入門項目 TiaoYu-1。 https://github.com/tiaoyu1122/TiaoYu-1 項目優點&#xff1a; 幾乎每一行代碼(一些重復的代碼除外)都添加了注釋&#xff0c;詳細介紹了代碼的作用&#xff0c;方便閱讀與理解。基本上覆蓋了常見 LLM 模型的全部訓練流程&#x…

Linux里more 和 less的區別

在 Linux/Unix 系統中&#xff0c;more 和 less 都是用于分頁查看文本文件的命令&#xff0c;但 less 是 more 的增強版&#xff0c;功能更強大。以下是它們的核心區別和用法對比&#xff1a; 1. 基礎功能對比 特性moreless&#xff08;更強大&#xff09;向前翻頁? 僅支持向…

基于PDF流式渲染的Word文檔在線預覽技術

一、背景介紹 在系統開發中&#xff0c;實現在線文檔預覽與編輯功能是許多項目的核心需求&#xff0c;但在實際的開發過程中&#xff0c;我們經常會面臨以下難點&#xff1a; 1&#xff09;格式兼容性問題&#xff1a;瀏覽器原生不支持解析Word二進制格式&#xff0c;直接渲染會…

ai學習--python部分-1.變量名及命名空間的存儲

初學代碼時總有一個問題困擾我&#xff1a;a 10 # a指向地址0x1234&#xff08;存儲10&#xff09; 變量a的值10存儲在0x1234&#xff0c;那么變量a需要存儲嗎&#xff1f;a又存儲在什么地址呢 目錄 1. ??命名空間的本質?? 2. ??命名空間的內存占用?? 3. ??…

Leetcode 3563. Lexicographically Smallest String After Adjacent Removals

Leetcode 3563. Lexicographically Smallest String After Adjacent Removals 1. 解題思路2. 代碼實現 題目鏈接&#xff1a;3563. Lexicographically Smallest String After Adjacent Removals 1. 解題思路 這次的最后一題同樣沒有自力搞定&#xff0c;簡直了…… 這道題還…