自己写的 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 下面。

原文地址:https://www.cnblogs.com/z5337/p/4688204.html