自己写的 读写 ini 配置文件类
/// <summary> /// 不调用系统API 读写 ini 配置文件 /// </summary> public class RW_ini { #region ========ini 读写======== // 首次调用 RWini 时需要初始化此参数 public static string pathIni; // 记录错误信息 与 WriteLog 一起使用 public static string pathErr; public static string ReadIni(string section, string key) { string result = ""; try { // 文件存在 if (File.Exists(pathIni)) { //读取INI文件 string buffer; string[] temp; using (StreamReader sr = new StreamReader(pathIni, Encoding.Default)) { while (sr.Peek() >= 0) { buffer = sr.ReadLine().Trim(); if (!string.IsNullOrEmpty(buffer) && buffer.StartsWith(string.Format("[{0}]", section), StringComparison.OrdinalIgnoreCase)) { while (sr.Peek() > 0) { buffer = sr.ReadLine().Trim(); if (!string.IsNullOrEmpty(buffer)) { if (buffer.StartsWith("[")) break; if (!buffer.StartsWith(";")) { temp = buffer.Split('='); if (temp.Length > 1 && temp[0].TrimEnd().Equals(key, StringComparison.OrdinalIgnoreCase)) { return temp[1].TrimStart(); } } } } // 不再判断下一个节点 return result; } } } } // 不再判断下一个节点 return result; } catch { throw new ApplicationException("同步文件读取异常!"); } } private void WriteIni(string section, string key, string value) { try { // 文件不存在则创建 if (!File.Exists(pathIni)) { File.Create(pathIni); } // 修改INI文件 bool modify = false, find = false; string buffer; List<string> temp = new List<string>(); // 将所有配置文件放到集合中 using (StreamReader sr = new StreamReader(pathIni, Encoding.Default)) { while (sr.Peek() >= 0) { buffer = sr.ReadLine().Trim(); temp.Add(buffer); if (!modify && !string.IsNullOrEmpty(buffer) && buffer.StartsWith(string.Format("[{0}]", section), StringComparison.OrdinalIgnoreCase)) { find = true; while (sr.Peek() >= 0) { buffer = sr.ReadLine().Trim(); temp.Add(buffer); if (!string.IsNullOrEmpty(buffer)) { if (buffer.StartsWith("[")) break; if (!modify && !buffer.StartsWith(";") && buffer.Split('=')[0].TrimEnd() == key) { modify = true; temp.RemoveAt(temp.Count - 1); if (value != null) { temp.Add(string.Format("{0} = {1}", key, value)); } } } } if (!modify) { temp.Insert(temp.Count - 1, string.Format("{0} = {1}", key, value)); } } } if (!modify && !find) { temp.Add(string.Format("[{0}]", section)); temp.Add(string.Format("{0} = {1}", key, value)); } } using (StreamWriter sw = new StreamWriter(pathIni, false, Encoding.Default)) { foreach (string item in temp) sw.WriteLine(item); } } catch { throw new ApplicationException("同步文件写入异常!"); } } public static void WriteLog(string content) { File.AppendText(pathErr).WriteLine("时间:{0} 信息:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm"), content); } }
因为 WinCE 不带读写 ini 的API 而且自己用的配置文件节点并不多
因此自己编写此类 用于 WinCE 系统中。C# 代码
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!