/// <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() >= )
{
buffer = sr.ReadLine().Trim();
if (!string.IsNullOrEmpty(buffer) && buffer.StartsWith(string.Format("[{0}]", section), StringComparison.OrdinalIgnoreCase))
{
while (sr.Peek() > )
{
buffer = sr.ReadLine().Trim();
if (!string.IsNullOrEmpty(buffer))
{
if (buffer.StartsWith("["))
break;
if (!buffer.StartsWith(";"))
{
temp = buffer.Split('=');
if (temp.Length > && temp[].TrimEnd().Equals(key, StringComparison.OrdinalIgnoreCase))
{
return temp[].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() >= )
{
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() >= )
{
buffer = sr.ReadLine().Trim();
temp.Add(buffer);
if (!string.IsNullOrEmpty(buffer))
{
if (buffer.StartsWith("["))
break;
if (!modify && !buffer.StartsWith(";") && buffer.Split('=')[].TrimEnd() == key)
{
modify = true;
temp.RemoveAt(temp.Count - );
if (value != null)
{
temp.Add(string.Format("{0} = {1}", key, value));
}
}
}
} if (!modify)
{
temp.Insert(temp.Count - , 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# 代码

自己写的 读写 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. mybatis左连接需要输出左表的指定内容与筛选

    SELECT rpl.ID, rpl.DID, rpl.TRADE_TYPE, rpl.TRADE_TIME, rpl.CALL_TIME, rpl.TRADE_ADDR, rpl.RECEIVE_P ...

  2. struts2.properties

    #action后缀struts.action.extension=action#上传文件的工作目录与文件的最大尺寸struts.multipart.saveDir=struts.multipart.m ...

  3. hibernate 各种主键生成策略(转)

    http://www.cnblogs.com/kakafra/archive/2012/09/16/2687569.html 1.assigned 主键由外部程序负责生成,在 save() 之前必须指 ...

  4. 0122有关List、Set、Map的练习

    import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; public class SZYL { ...

  5. 一款经典的 jQuery Lightbox 灯箱效果

    一个灯箱效果的图片展示插件. 版本: jQuery v1.2.3+ jQuery Lightbox v2.7.1 github 实例预览 使用方法 载入 CSS 文件 <link rel=&qu ...

  6. WEB接口测试之Jmeter接口测试自动化 (四)

    Jmeter是压力测试.接口测试工具,Ant是基于Java的构建工具,具有跨平台的作用,jenkins是持续集成工具.将这三者结合起来可以搭建一套webservice接口测试的持续构建环境.   1. ...

  7. OC基础:类和对象 分类: ios学习 OC 2015-06-12 18:55 17人阅读 评论(0) 收藏

    OC:Objective-c     面向对象的c语言,简称obj-c或者OC OC和C的区别 1.OC是C语言的超集,OC是在C语言的基础上结合smalltalk的优点,开发出来的语言.oc兼容所有 ...

  8. 最大似然估计和最大后验概率MAP

    最大似然估计是一种奇妙的东西,我觉得发明这种估计的人特别才华.如果是我,觉得很难凭空想到这样做. 极大似然估计和贝叶斯估计分别代表了频率派和贝叶斯派的观点.频率派认为,参数是客观存在的,只是未知而矣. ...

  9. c++野(wild)指针与悬空(dangling)指针

    re 1.https://www.cnblogs.com/idorax/p/6475941.html end

  10. opencv-python教程学习系列13-图像平滑

    前言 opencv-python教程学习系列记录学习python-opencv过程的点滴,本文主要介绍图像平滑,坚持学习,共同进步. 系列教程参照OpenCV-Python中文教程: 系统环境 系统: ...