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.gifContractedBlock.gif
dot.gif {    
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// IniFiles的类
ExpandedSubBlockEnd.gif  
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gif  public class IniFiles dot.gif{
InBlock.gif    
public string FileName; //INI文件名
InBlock.gif    
//声明读写INI文件的API函数
InBlock.gif
    [DllImport("kernel32")]
InBlock.gif    
private static extern bool WritePrivateProfileString(string section,string key,string val,string filePath);
InBlock.gif    [DllImport(
"kernel32")]
InBlock.gif    
private 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.gif        
trydot.gif{
InBlock.gif          sw.Write(
"#表格配置档案");
InBlock.gif          sw.Close();
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
catchdot.gif{
InBlock.gif          
throw(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.gif      
if (!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.gif      
int 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.gif      
return s.Trim();
ExpandedSubBlockEnd.gif    }

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

ExpandedSubBlockStart.gifContractedSubBlock.gif      
catch (Exception ex)dot.gif{
InBlock.gif        Console.WriteLine(ex.Message);
InBlock.gif        
return 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.gif      
try dot.gif{
InBlock.gif        
return Convert.ToBoolean(ReadString(Section, Ident, Convert.ToString(Default) ));
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockStart.gifContractedSubBlock.gif      
catch (Exception ex)dot.gif{
InBlock.gif        Console.WriteLine(ex.Message);
InBlock.gif        
return 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.gif      
int bufLen=GetPrivateProfileString(Section, nullnull, Buffer, Buffer.GetUpperBound(0),
InBlock.gif        FileName);
InBlock.gif      
//对Section进行解析
InBlock.gif
      GetStringsFromBuffer(Buffer, bufLen, Idents);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
private void GetStringsFromBuffer(Byte[] Buffer, int bufLen, StringCollection Strings) dot.gif{
InBlock.gif      Strings.Clear();
ExpandedSubBlockStart.gifContractedSubBlock.gif      
if (bufLen!=0dot.gif{
InBlock.gif        
int start=0;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
for(int i=0; i < bufLen; i++dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif          
if ((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.gif      
int bufLen=0;
InBlock.gif      bufLen 
= GetPrivateProfileString(nullnullnull,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.gif      
foreach(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, nullnull, FileName)) dot.gif{
InBlock.gif        
throw(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(
nullnullnull, 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.gif      
return 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}

None.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值