当前位置: 首页 > news >正文

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.xdnf.cn/news/652447.html

相关文章:

  • VR 技术在农业领域或许是一抹新曙光​
  • Java Class 文件编码机制全解析
  • 分布式锁与锁续期
  • 轻量级视觉语言模型 Dolphin:高效精准的文档结构化解析利器
  • 电机控制学习笔记
  • 深入解析Spring Boot与Spring Security整合实现JWT认证
  • ADS学习笔记(四) S参数仿真
  • 网络编程1
  • SAP ERP 系统拆分的七大挑战
  • WIN--文件读写
  • Linux的top命令使用
  • 在前端项目中实现打包后可配置地址(如 API 域名、静态资源路径等)
  • 告别复杂操作!链抽象如何让 Web3 用户体验媲美 Web2?
  • Element UI 对话框固定宽度 + 遮罩层深度定制方案
  • 零基础设计模式——结构型模式 - 适配器模式
  • 基于 docker 部署 k8s 集群
  • 机器学习中的线性回归:从理论到实践的深度解析
  • 运行comfyui Wan2.1 文生视频工作流,问题总结
  • vue3+vite项目中使用Tailwind CSS
  • 鸿蒙OSUniApp 制作个性化的评分星级组件#三方框架 #Uniapp
  • 力扣刷题Day 56:岛屿数量(200)
  • 多线程(5)——单例模式,阻塞队列
  • C++多态与虚函数
  • UR10e 机器人如何通过扭矩控制接口实现高效装配
  • window 显示驱动开发-呈现开销改进
  • 如何在 Django 中集成 MCP Server
  • Leetcode 3556. Sum of Largest Prime Substrings
  • TPAMI 2025 | CEM:使用因果效应图解释底层视觉模型
  • Hive 分区详解:从基础概念到实战应用
  • R 语言科研绘图 --- 热力图-汇总