1. /// <summary>
  2. /// 不调用系统API 读写 ini 配置文件
  3. /// </summary>
  4. public class RW_ini
  5. {
  6. #region ========ini 读写========
  7. // 首次调用 RWini 时需要初始化此参数
  8. public static string pathIni;
  9. // 记录错误信息 与 WriteLog 一起使用
  10. public static string pathErr;
  11.  
  12. public static string ReadIni(string section, string key)
  13. {
  14. string result = "";
  15. try
  16. {
  17. // 文件存在
  18. if (File.Exists(pathIni))
  19. {
  20. //读取INI文件
  21. string buffer;
  22. string[] temp;
  23. using (StreamReader sr = new StreamReader(pathIni, Encoding.Default))
  24. {
  25. while (sr.Peek() >= )
  26. {
  27. buffer = sr.ReadLine().Trim();
  28. if (!string.IsNullOrEmpty(buffer) && buffer.StartsWith(string.Format("[{0}]", section), StringComparison.OrdinalIgnoreCase))
  29. {
  30. while (sr.Peek() > )
  31. {
  32. buffer = sr.ReadLine().Trim();
  33. if (!string.IsNullOrEmpty(buffer))
  34. {
  35. if (buffer.StartsWith("["))
  36. break;
  37. if (!buffer.StartsWith(";"))
  38. {
  39. temp = buffer.Split('=');
  40. if (temp.Length > && temp[].TrimEnd().Equals(key, StringComparison.OrdinalIgnoreCase))
  41. {
  42. return temp[].TrimStart();
  43. }
  44. }
  45. }
  46. }
  47. // 不再判断下一个节点
  48. return result;
  49. }
  50. }
  51. }
  52. }
  53. // 不再判断下一个节点
  54. return result;
  55. }
  56. catch
  57. {
  58. throw new ApplicationException("同步文件读取异常!");
  59. }
  60. }
  61.  
  62. private void WriteIni(string section, string key, string value)
  63. {
  64. try
  65. {
  66. // 文件不存在则创建
  67. if (!File.Exists(pathIni))
  68. {
  69. File.Create(pathIni);
  70. }
  71. // 修改INI文件
  72. bool modify = false, find = false;
  73. string buffer;
  74. List<string> temp = new List<string>();
  75. // 将所有配置文件放到集合中
  76. using (StreamReader sr = new StreamReader(pathIni, Encoding.Default))
  77. {
  78. while (sr.Peek() >= )
  79. {
  80. buffer = sr.ReadLine().Trim();
  81. temp.Add(buffer);
  82.  
  83. if (!modify && !string.IsNullOrEmpty(buffer) && buffer.StartsWith(string.Format("[{0}]", section), StringComparison.OrdinalIgnoreCase))
  84. {
  85. find = true;
  86. while (sr.Peek() >= )
  87. {
  88. buffer = sr.ReadLine().Trim();
  89. temp.Add(buffer);
  90. if (!string.IsNullOrEmpty(buffer))
  91. {
  92. if (buffer.StartsWith("["))
  93. break;
  94. if (!modify && !buffer.StartsWith(";") && buffer.Split('=')[].TrimEnd() == key)
  95. {
  96. modify = true;
  97. temp.RemoveAt(temp.Count - );
  98. if (value != null)
  99. {
  100. temp.Add(string.Format("{0} = {1}", key, value));
  101. }
  102. }
  103. }
  104. }
  105.  
  106. if (!modify)
  107. {
  108. temp.Insert(temp.Count - , string.Format("{0} = {1}", key, value));
  109. }
  110. }
  111. }
  112. if (!modify && !find)
  113. {
  114. temp.Add(string.Format("[{0}]", section));
  115. temp.Add(string.Format("{0} = {1}", key, value));
  116. }
  117. }
  118. using (StreamWriter sw = new StreamWriter(pathIni, false, Encoding.Default))
  119. {
  120. foreach (string item in temp)
  121. sw.WriteLine(item);
  122. }
  123. }
  124. catch
  125. {
  126. throw new ApplicationException("同步文件写入异常!");
  127. }
  128. }
  129.  
  130. public static void WriteLog(string content)
  131. {
  132. File.AppendText(pathErr).WriteLine("时间:{0} 信息:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm"), content);
  133. }
  134. }

因为 WinCE 不带读写 ini 的API 而且自己用的配置文件节点并不多

因此自己编写此类 用于 WinCE 系统中。C# 代码

自己写的 读写 ini 配置文件类的更多相关文章

  1. 引用“kernel32”读写ini配置文件

    引用"kernel32"读写ini配置文件 unity ini kernel32 配置文件  引用"kernel32"读写ini配置文件 OverView ke ...

  2. C# 文件的一些基本操作(转)//用C#读写ini配置文件

    C# 文件的一些基本操作 2009-07-19  来自:博客园  字体大小:[大 中 小] 摘要:介绍C#对文件的一些基本操作,读写等. using System;using System.IO;us ...

  3. 使用C#读写ini配置文件

    INI就是扩展名为"INI"的文件,其实他本身是个文本文件,可以用记事本打工,主要存放的是用户所做的选择或系统的各种参数. INI文件其实并不是普通的文本文件.它有自己的结构.由若 ...

  4. C# 读写 ini 配置文件

    虽说 XML 文件越发流行,但精简的 ini 配置文件还是经常会用到,在此留个脚印. 当然,文中只是调用系统API,不会报错,如有必要,也可以直接以流形式读取 ini文件并解析. /// <su ...

  5. C#操作读写INI配置文件

    一个完整的INI文件格式由节(section).键(key).值(value)组成.示例如:[section]key1=value1key2=value2; 备注:value的值不要太长,理论上最多不 ...

  6. [转]VB 读写ini 配置文件

    转自 百度知道 C# 读写 ini配置文件 点此链接 'API 声明Public Declare Function GetPrivateProfileString Lib "kernel32 ...

  7. C# 读取Ini配置文件类

    配置文件 为fileName.ini 的文件 第一行必须为空,不然读不出值 [section1] key=value key2=value ......... [section2] key=value ...

  8. C++读写ini配置文件GetPrivateProfileString()&WritePrivateProfileString()

    转载: 1.https://blog.csdn.net/fengbingchun/article/details/6075716 2. 转自:http://hi.baidu.com/andywangc ...

  9. QT读写ini配置文件

        /********下面是写ini文件*************************/     //Qt中使用QSettings类读写ini文件     //QSettings构造函数的第一 ...

随机推荐

  1. 2015年蓝桥杯省赛B组第3题--三羊献瑞

    三羊献瑞 观察下面的加法算式: 祥 瑞 生 辉   +   三 羊 献 瑞 -------------------    三 羊 生 瑞 气 (如果有对齐问题,可以参看[图1.jpg]) 其中,相同的 ...

  2. android开源项目集合

    ZXing http://code.google.com/p/zxing/ 条形码.二维码 K-9 Mail http://code.google.com/p/k9mail/ 邮件客户端 Sipdro ...

  3. SOD范例

    SOD申请台站波形数据范例: <?xml version="1.0"?> <sod> <eventArm> <fdsnEvent> ...

  4. Jmeter BeanShell 从数据库中获取数据并创建一个requesBody

    一.前言 在测试接口的时候常常会使用到数据库中的数据,当要使用大量的数据时,仅仅使用数据库查询是不够的.还需要使用自动化让操作更简便. 下面以一个简单的例子阐述一下如何使用beanShell让代码更简 ...

  5. mysql 试图

    关系型数据库中的数据是由一张一张的二维关系表所组成,简单的单表查询只需要遍历一个表,而复杂的多表查询需要将多个表连接起来进行查询任务.对于复杂的查询事件,每次查询都需要编写MySQL代码效率低下.为了 ...

  6. C语言基础:常见循环语句 分类: iOS学习 c语言基础 2015-06-10 21:46 13人阅读 评论(0) 收藏

    for语句 for( 初始化表达式; 循环判断条件  ;增量表达式); while(条件表达式){ 循环体; } 先判断条件表达式,如果为真就执行循环体,执行完再去判断条件表达式 do{ 循环体; } ...

  7. 基础练习 Huffuman树

     基础练习 Huffuman树   时间限制:1.0s   内存限制:512.0MB        问题描述 Huffman树在编码中有着广泛的应用.在这里,我们只关心Huffman树的构造过程. 给 ...

  8. 关于CGI和FastCGI的理解

    在搭建 LAMP/LNMP 服务器时,会经常遇到 PHP-FPM.FastCGI和CGI 这几个概念.如果对它们一知半解,很难搭建出高性能的服务器. 0.CGI的引入 在网站的整体架构中,Web Se ...

  9. JAVA正则表达式-捕获组与非捕获组

    Java捕获组与非捕获组的问题 先看例子: import java.util.regex.Matcher; import java.util.regex.Pattern; public class P ...

  10. LM算法的推导过程