随笔 - 750  文章 - 1  评论 - 107  阅读 - 34万

自己写的 Readini 类

复制代码
 1 using System;
 2 using System.IO;
 3 using System.Runtime.InteropServices;
 4 using System.Text;
 5 using System.Windows.Forms;
 6 
 7 namespace UtilityClass
 8 {
 9     /// <summary>
10     /// 不使用系统API 读 ini 配置文件
11     /// </summary>
12     public class RW_ini
13     {
14         #region ========ini 读写========
15         // 首次调用 RWini 时需要初始化此参数
16         public static string pathIni;
17         // 记录错误信息 与 WriteLog 一起使用
18         public static string pathErr;
19 
20         public static string ReadIni(string section, string key)
21         {
22             string result = "2015-07-25 08:00:00";
23             try
24             {
25                 // 文件不存在则创建
26                 if (!File.Exists(pathIni))
27                 {
28                     File.Create(pathIni);
29                 }
30                 else
31                 {
32                     //读取INI文件
33                     string buffer;
34                     string[] temp;
35                     using (StreamReader sr = new StreamReader(pathIni, Encoding.Default))
36                     {
37                         while (sr.Peek() >= 0)
38                         {
39                             buffer = sr.ReadLine().Trim();
40                             if (!string.IsNullOrEmpty(buffer) && buffer.StartsWith(string.Format("[{0}]", section)))
41                             {
42                                 while (sr.Peek() > 0)
43                                 {
44                                     buffer = sr.ReadLine().Trim();
45                                     if (!string.IsNullOrEmpty(buffer) && !buffer.StartsWith(";"))
46                                     {
47                                         temp = buffer.Split('=');
48                                         if (temp.Length > 1 && temp[0].TrimEnd().Equals(key, StringComparison.OrdinalIgnoreCase))
49                                         {
50                                             return temp[1].TrimStart();
51                                         }
52                                     }
53                                 }
54                                 // 不再判断下一个节点
55                                 return result;
56                             }
57                         }
58                     }
59                 }
60                 // 不再判断下一个节点
61                 return result;
62             }
63             catch
64             {
65                 throw new ApplicationException("同步文件读取异常!");
66             }
67         }
68 
69         //public static void WriteIni(string section, string key, string value)
70         //{
71         //    throw new NotImplementedException();
72         //}
73 
74         public static void WriteLog(string content)
75         {
76             File.AppendText(pathErr).WriteLine("时间:{0} 信息:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm"), content);
77         }
78     }
79 }
复制代码

可运行在 Win CE 5.0 下面。

posted on   z5337  阅读(470)  评论(2编辑  收藏  举报
编辑推荐:
· 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 让容器管理更轻松!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示