C#一个Ini操作类

简介:

注:原创作者我不记得了,反正不是我,我原来是搞delphi的,编写这个代码的作者也是写delphi,所以我喜欢用这个类,和delphi提供的TIniFile一样的用法。以下是源码,带全部注解。
None.gif using System;
None.gif using System.IO;
None.gif using System.Runtime.InteropServices;
None.gif using System.Text;
None.gif using System.Collections;
None.gif using System.Collections.Specialized;
None.gif
None.gif namespace DataGridEx
ExpandedBlockStart.gif ContractedBlock.gif dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// IniFiles的类
ExpandedSubBlockEnd.gif
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gifpublic class IniFiles dot.gif{
InBlock.gifpublic string FileName; //INI文件名
InBlock.gif
//声明读写INI文件的API函数
InBlock.gif
[DllImport("kernel32")]
InBlock.gifprivate static extern bool WritePrivateProfileString(string section,string key,string val,string filePath);
InBlock.gif [DllImport("kernel32")]
InBlock.gifprivate static extern int GetPrivateProfileString(string section,string key,string def, byte[] retVal,int size,string filePath);
InBlock.gif//类的构造函数,传递INI文件名
ExpandedSubBlockStart.gifContractedSubBlock.gif
public IniFiles(string AFileName) dot.gif{
InBlock.gif// 判断文件是否存在
InBlock.gif
FileInfo fileInfo=new FileInfo(AFileName);
InBlock.gif//Todo:搞清枚举的用法
ExpandedSubBlockStart.gifContractedSubBlock.gif
if ((!fileInfo.Exists))dot.gif{ //|| (FileAttributes.Directory in fileInfo.Attributes))
InBlock.gif
//文件不存在,建立文件
InBlock.gif
System.IO.StreamWriter sw = new System.IO.StreamWriter(AFileName,false,System.Text.Encoding.Default);
ExpandedSubBlockStart.gifContractedSubBlock.giftrydot.gif{
InBlock.gif sw.Write("#表格配置档案");
InBlock.gif sw.Close();
ExpandedSubBlockStart.gifContractedSubBlock.gif }
catchdot.gif{
InBlock.gifthrow(new ApplicationException("Ini文件不存在"));
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif//必须是完全路径,不能是相对路径
InBlock.gif
FileName = fileInfo.FullName;
ExpandedSubBlockEnd.gif }

InBlock.gif//写INI文件
ExpandedSubBlockStart.gifContractedSubBlock.gif
public void WriteString(string Section,string Ident,string Value) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gifif (!WritePrivateProfileString(Section, Ident,Value,FileName)) dot.gif{
InBlock.gif// Todo:抛出自定义的异常
InBlock.gif
throw(new ApplicationException("写Ini文件出错"));
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif//读取INI文件指定
ExpandedSubBlockStart.gifContractedSubBlock.gif
public string ReadString(string Section,string Ident, string Default) dot.gif{
InBlock.gif Byte[] Buffer=new Byte[65535];
InBlock.gifint bufLen=GetPrivateProfileString(Section,Ident,Default,Buffer, Buffer.GetUpperBound(0),FileName);
InBlock.gif//必须设定0(系统默认的代码页)的编码方式,否则无法支持中文
InBlock.gif
string s=Encoding.GetEncoding(0).GetString(Buffer);
InBlock.gif s=s.Substring(0,bufLen);
InBlock.gifreturn s.Trim();
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif//读整数
ExpandedSubBlockStart.gifContractedSubBlock.gif
public int ReadInteger(string Section, string Ident , int Default)dot.gif{
InBlock.gifstring intStr=ReadString(Section, Ident, Convert.ToString(Default));
ExpandedSubBlockStart.gifContractedSubBlock.giftrydot.gif{
InBlock.gifreturn Convert.ToInt32(intStr);
ExpandedSubBlockEnd.gif }

ExpandedSubBlockStart.gifContractedSubBlock.gifcatch (Exception ex)dot.gif{
InBlock.gif Console.WriteLine(ex.Message);
InBlock.gifreturn Default;
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif//写整数
ExpandedSubBlockStart.gifContractedSubBlock.gif
public void WriteInteger(string Section,string Ident, int Value)dot.gif{
InBlock.gif WriteString(Section, Ident, Value.ToString());
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif//读布尔
ExpandedSubBlockStart.gifContractedSubBlock.gif
public bool ReadBool(string Section, string Ident, bool Default)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.giftry dot.gif{
InBlock.gifreturn Convert.ToBoolean(ReadString(Section, Ident, Convert.ToString(Default) ));
ExpandedSubBlockEnd.gif }

ExpandedSubBlockStart.gifContractedSubBlock.gifcatch (Exception ex)dot.gif{
InBlock.gif Console.WriteLine(ex.Message);
InBlock.gifreturn Default;
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif//写Bool
ExpandedSubBlockStart.gifContractedSubBlock.gif
public void WriteBool(string Section, string Ident , bool Value)dot.gif{
InBlock.gif WriteString(Section, Ident, Convert.ToString(Value));
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif//从Ini文件中,将指定的Section名称中的所有Ident添加到列表中
ExpandedSubBlockStart.gifContractedSubBlock.gif
public void ReadSection(string Section, StringCollection Idents) dot.gif{
InBlock.gif Byte[] Buffer=new Byte[16384];
InBlock.gif//Idents.Clear();
InBlock.gif

InBlock.gifint bufLen=GetPrivateProfileString(Section, null, null, Buffer, Buffer.GetUpperBound(0),
InBlock.gif FileName);
InBlock.gif//对Section进行解析
InBlock.gif
GetStringsFromBuffer(Buffer, bufLen, Idents);
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifprivate void GetStringsFromBuffer(Byte[] Buffer, int bufLen, StringCollection Strings) dot.gif{
InBlock.gif Strings.Clear();
ExpandedSubBlockStart.gifContractedSubBlock.gifif (bufLen!=0) dot.gif{
InBlock.gifint start=0;
ExpandedSubBlockStart.gifContractedSubBlock.giffor(int i=0; i < bufLen; i++) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gifif ((Buffer[i] == 0) && ((i-start)>0)) dot.gif{
InBlock.gif String s=Encoding.GetEncoding(0).GetString(Buffer, start, i-start);
InBlock.gif Strings.Add(s);
InBlock.gif start=i+1;
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif//从Ini文件中,读取所有的Sections的名称
ExpandedSubBlockStart.gifContractedSubBlock.gif
public void ReadSections(StringCollection SectionList) dot.gif{
InBlock.gif//Note:必须得用Bytes来实现,StringBuilder只能取到第一个Section
InBlock.gif
byte[] Buffer = new byte[65535];
InBlock.gifint bufLen=0;
InBlock.gif bufLen = GetPrivateProfileString(null, null, null,Buffer,
InBlock.gif Buffer.GetUpperBound(0), FileName);
InBlock.gif GetStringsFromBuffer(Buffer, bufLen, SectionList);
ExpandedSubBlockEnd.gif }

InBlock.gif//读取指定的Section的所有Value到列表中
ExpandedSubBlockStart.gifContractedSubBlock.gif
public void ReadSectionValues(string Section, NameValueCollection Values) dot.gif{
InBlock.gif StringCollection KeyList=new StringCollection();
InBlock.gif ReadSection(Section, KeyList);
InBlock.gif Values.Clear();
ExpandedSubBlockStart.gifContractedSubBlock.gifforeach(string key in KeyList) dot.gif{
InBlock.gif Values.Add(key, ReadString(Section, key, ""));
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif//清除某个Section
ExpandedSubBlockStart.gifContractedSubBlock.gif
public void EraseSection(string Section) dot.gif{
InBlock.gif//
ExpandedSubBlockStart.gifContractedSubBlock.gif
if (!WritePrivateProfileString(Section, null, null, FileName)) dot.gif{
InBlock.gifthrow(new ApplicationException("无法清除Ini文件中的Section"));
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif//删除某个Section下的键
ExpandedSubBlockStart.gifContractedSubBlock.gif
public void DeleteKey(string Section, string Ident) dot.gif{
InBlock.gif WritePrivateProfileString(Section, Ident, null, FileName);
ExpandedSubBlockEnd.gif }

InBlock.gif//Note:对于Win9X,来说需要实现UpdateFile方法将缓冲中的数据写入文件
InBlock.gif
//在Win NT, 2000和XP上,都是直接写文件,没有缓冲,所以,无须实现UpdateFile
InBlock.gif
//执行完对Ini文件的修改之后,应该调用本方法更新缓冲区。
ExpandedSubBlockStart.gifContractedSubBlock.gif
public void UpdateFile() dot.gif{
InBlock.gif WritePrivateProfileString(null, null, null, FileName);
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif//检查某个Section下的某个键值是否存在
ExpandedSubBlockStart.gifContractedSubBlock.gif
public bool ValueExists(string Section, string Ident) dot.gif{
InBlock.gif//
InBlock.gif
StringCollection Idents=new StringCollection();
InBlock.gif ReadSection(Section, Idents);
InBlock.gifreturn Idents.IndexOf(Ident)>-1;
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif//确保资源的释放
ExpandedSubBlockStart.gifContractedSubBlock.gif
~IniFiles()dot.gif{
InBlock.gif UpdateFile();
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}



本文转自suifei博客园博客,原文链接:http://www.cnblogs.com/Chinasf/archive/2005/04/16/138744.html,如需转载请自行联系原作者
相关文章
|
1月前
|
C#
C#学习相关系列之数据类型类的三大特性(二)
C#学习相关系列之数据类型类的三大特性(二)
|
1月前
|
C#
58.c#:directory类
58.c#:directory类
12 0
|
1月前
|
C#
57.c#:directorylnfo类
57.c#:directorylnfo类
13 0
|
1月前
|
监控 C#
55.c#:file类
55.c#:file类
16 1
|
1月前
|
算法 C#
54.c#:random类
54.c#:random类
14 1
|
1月前
|
C#
51.c#:string类的静态方法
51.c#:string类的静态方法
20 1
|
1月前
|
C#
27.c#关键字sealed修饰类
27.c#关键字sealed修饰类
12 0
|
3月前
|
Java C#
C# 面向对象编程解析:优势、类和对象、类成员详解
OOP代表面向对象编程。 过程式编程涉及编写执行数据操作的过程或方法,而面向对象编程涉及创建包含数据和方法的对象。 面向对象编程相对于过程式编程具有几个优势: OOP执行速度更快,更容易执行 OOP为程序提供了清晰的结构 OOP有助于保持C#代码DRY("不要重复自己"),并使代码更易于维护、修改和调试 OOP使得能够创建完全可重用的应用程序,编写更少的代码并减少开发时间 提示:"不要重复自己"(DRY)原则是有关减少代码重复的原则。应该提取出应用程序中常见的代码,并将其放置在单一位置并重复使用,而不是重复编写。
51 0
|
1月前
|
C#
深入C#中的String类
深入C#中的String类
11 0
|
1月前
|
C#
C#学习系列相关之多线程(二)----Thread类介绍
C#学习系列相关之多线程(二)----Thread类介绍