码迷,mamicode.com
首页 > 其他好文 > 详细

C# 读写INI 文件

时间:2014-09-25 17:05:29      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   strong   for   

INI 格式:

[Section1]  

    KeyWord1 = Value1  

    KeyWord2 = Value2  

    ...  

[Section2]  

  KeyWord3 = Value3  

  KeyWord4 = Value4 

 

 

  1. public class INIClass  
  2. {  
  3.  public string inipath;  
  4.  [DllImport("kernel32")]  
  5.  private static extern long WritePrivateProfileString(
  6. string section,string key,string val,string filePath);  
  7.  [DllImport("kernel32")]  
  8.  private static extern int GetPrivateProfileString(
  9. string section,string key,
  10. string def,StringBuilder retVal,
  11. int size,string filePath);  
  12.  /// ﹤summary﹥  
  13.  /// 构造方法  
  14.  /// ﹤/summary﹥  
  15.  /// ﹤param name="INIPath"﹥文件路径﹤/param﹥  
  16.  public INIClass(string INIPath)  
  17.  {  
  18.   inipath = INIPath;  
  19.  }  
  20.  /// ﹤summary﹥  
  21.  /// 写入INI文件  
  22.  /// ﹤/summary﹥  
  23.  /// ﹤param name="Section"﹥项目名称(如 [TypeName] )﹤/param﹥  
  24.  /// ﹤param name="Key"﹥键﹤/param﹥  
  25.  /// ﹤param name="Value"﹥值﹤/param﹥  
  26.  public void IniWriteValue(string Section,string Key,string Value)  
  27.  {  
  28.   WritePrivateProfileString(Section,Key,Value,this.inipath);  
  29.  }  
  30.  /// ﹤summary﹥  
  31.  /// 读出INI文件  
  32.  /// ﹤/summary﹥  
  33.  /// ﹤param name="Section"﹥项目名称(如 [TypeName] )﹤/param﹥  
  34.  /// ﹤param name="Key"﹥键﹤/param﹥  
  35.  public string IniReadValue(string Section,string Key)  
  36.  {  
  37.   StringBuilder temp = new StringBuilder(500);  
  38.   int i = GetPrivateProfileString(Section,Key,"",temp,500,this.inipath);  
  39.   return temp.ToString();  
  40.  }  
  41.  /// ﹤summary﹥  
  42.  /// 验证文件是否存在  
  43.  /// ﹤/summary﹥  
  44.  /// ﹤returns﹥布尔值﹤/returns﹥  
  45.  public bool ExistINIFile()  
  46.  {  
  47.   return File.Exists(inipath);  
  48.  }  

C# ini文件读写类

using System;
bubuko.com,布布扣using System.IO;
bubuko.com,布布扣using System.Runtime.InteropServices;
bubuko.com,布布扣using System.Text;
bubuko.com,布布扣using System.Collections;
bubuko.com,布布扣using System.Collections.Specialized;
bubuko.com,布布扣
bubuko.com,布布扣namespace wuyisky{
bubuko.com,布布扣  /**/
bubuko.com,布布扣  /// <summary>
bubuko.com,布布扣  /// IniFiles的类
bubuko.com,布布扣  /// </summary>
bubuko.com,布布扣  public class IniFiles
bubuko.com,布布扣  {
bubuko.com,布布扣    public string FileName; //INI文件名
bubuko.com,布布扣    //声明读写INI文件的API函数
bubuko.com,布布扣    [DllImport("kernel32")]
bubuko.com,布布扣    private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath);
bubuko.com,布布扣    [DllImport("kernel32")]
bubuko.com,布布扣    private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);
bubuko.com,布布扣    //类的构造函数,传递INI文件名
bubuko.com,布布扣    public IniFiles(string AFileName)
bubuko.com,布布扣    {
bubuko.com,布布扣      // 判断文件是否存在
bubuko.com,布布扣      FileInfo fileInfo = new FileInfo(AFileName);
bubuko.com,布布扣      //Todo:搞清枚举的用法
bubuko.com,布布扣      if ((!fileInfo.Exists))
bubuko.com,布布扣      { //|| (FileAttributes.Directory in fileInfo.Attributes))
bubuko.com,布布扣        //文件不存在,建立文件
bubuko.com,布布扣        System.IO.StreamWriter sw = new System.IO.StreamWriter(AFileName, false, System.Text.Encoding.Default);
bubuko.com,布布扣        try
bubuko.com,布布扣        {
bubuko.com,布布扣          sw.Write("#表格配置档案");
bubuko.com,布布扣          sw.Close();
bubuko.com,布布扣        }
bubuko.com,布布扣
bubuko.com,布布扣        catch
bubuko.com,布布扣        {
bubuko.com,布布扣          throw (new ApplicationException("Ini文件不存在"));
bubuko.com,布布扣        }
bubuko.com,布布扣      }
bubuko.com,布布扣      //必须是完全路径,不能是相对路径
bubuko.com,布布扣      FileName = fileInfo.FullName;
bubuko.com,布布扣    }
bubuko.com,布布扣    //写INI文件
bubuko.com,布布扣    public void WriteString(string Section, string Ident, string Value)
bubuko.com,布布扣    {
bubuko.com,布布扣      if (!WritePrivateProfileString(Section, Ident, Value, FileName))
bubuko.com,布布扣      {
bubuko.com,布布扣 
bubuko.com,布布扣        throw (new ApplicationException("写Ini文件出错"));
bubuko.com,布布扣      }
bubuko.com,布布扣    }
bubuko.com,布布扣    //读取INI文件指定
bubuko.com,布布扣    public string ReadString(string Section, string Ident, string Default)
bubuko.com,布布扣    {
bubuko.com,布布扣      Byte[] Buffer = new Byte[65535];
bubuko.com,布布扣      int bufLen = GetPrivateProfileString(Section, Ident, Default, Buffer, Buffer.GetUpperBound(0), FileName);
bubuko.com,布布扣      //必须设定0(系统默认的代码页)的编码方式,否则无法支持中文
bubuko.com,布布扣      string s = Encoding.GetEncoding(0).GetString(Buffer);
bubuko.com,布布扣      s = s.Substring(0, bufLen);
bubuko.com,布布扣      return s.Trim();
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    //读整数
bubuko.com,布布扣    public int ReadInteger(string Section, string Ident, int Default)
bubuko.com,布布扣    {
bubuko.com,布布扣      string intStr = ReadString(Section, Ident, Convert.ToString(Default));
bubuko.com,布布扣      try
bubuko.com,布布扣      {
bubuko.com,布布扣        return Convert.ToInt32(intStr);
bubuko.com,布布扣
bubuko.com,布布扣      }
bubuko.com,布布扣      catch (Exception ex)
bubuko.com,布布扣      {
bubuko.com,布布扣        Console.WriteLine(ex.Message);
bubuko.com,布布扣        return Default;
bubuko.com,布布扣      }
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    //写整数
bubuko.com,布布扣    public void WriteInteger(string Section, string Ident, int Value)
bubuko.com,布布扣    {
bubuko.com,布布扣      WriteString(Section, Ident, Value.ToString());
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    //读布尔
bubuko.com,布布扣    public bool ReadBool(string Section, string Ident, bool Default)
bubuko.com,布布扣    {
bubuko.com,布布扣      try
bubuko.com,布布扣      {
bubuko.com,布布扣        return Convert.ToBoolean(ReadString(Section, Ident, Convert.ToString(Default)));
bubuko.com,布布扣      }
bubuko.com,布布扣      catch (Exception ex)
bubuko.com,布布扣      {
bubuko.com,布布扣        Console.WriteLine(ex.Message);
bubuko.com,布布扣        return Default;
bubuko.com,布布扣      }
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    //写Bool
bubuko.com,布布扣    public void WriteBool(string Section, string Ident, bool Value)
bubuko.com,布布扣    {
bubuko.com,布布扣      WriteString(Section, Ident, Convert.ToString(Value));
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    //从Ini文件中,将指定的Section名称中的所有Ident添加到列表中
bubuko.com,布布扣    public void ReadSection(string Section, StringCollection Idents)
bubuko.com,布布扣    {
bubuko.com,布布扣      Byte[] Buffer = new Byte[16384];
bubuko.com,布布扣      //Idents.Clear();
bubuko.com,布布扣
bubuko.com,布布扣      int bufLen = GetPrivateProfileString(Section, null, null, Buffer, Buffer.GetUpperBound(0),
bubuko.com,布布扣       FileName);
bubuko.com,布布扣      //对Section进行解析
bubuko.com,布布扣      GetStringsFromBuffer(Buffer, bufLen, Idents);
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    private void GetStringsFromBuffer(Byte[] Buffer, int bufLen, StringCollection Strings)
bubuko.com,布布扣    {
bubuko.com,布布扣      Strings.Clear();
bubuko.com,布布扣      if (bufLen != 0)
bubuko.com,布布扣      {
bubuko.com,布布扣        int start = 0;
bubuko.com,布布扣        for (int i = 0; i < bufLen; i++)
bubuko.com,布布扣        {
bubuko.com,布布扣          if ((Buffer[i] == 0) && ((i - start) > 0))
bubuko.com,布布扣          {
bubuko.com,布布扣            String s = Encoding.GetEncoding(0).GetString(Buffer, start, i - start);
bubuko.com,布布扣            Strings.Add(s);
bubuko.com,布布扣            start = i + 1;
bubuko.com,布布扣          }
bubuko.com,布布扣        }
bubuko.com,布布扣      }
bubuko.com,布布扣    }
bubuko.com,布布扣    //从Ini文件中,读取所有的Sections的名称
bubuko.com,布布扣    public void ReadSections(StringCollection SectionList)
bubuko.com,布布扣    {
bubuko.com,布布扣      //Note:必须得用Bytes来实现,StringBuilder只能取到第一个Section
bubuko.com,布布扣      byte[] Buffer = new byte[65535];
bubuko.com,布布扣      int bufLen = 0;
bubuko.com,布布扣      bufLen = GetPrivateProfileString(null, null, null, Buffer,
bubuko.com,布布扣       Buffer.GetUpperBound(0), FileName);
bubuko.com,布布扣      GetStringsFromBuffer(Buffer, bufLen, SectionList);
bubuko.com,布布扣    }
bubuko.com,布布扣    //读取指定的Section的所有Value到列表中
bubuko.com,布布扣    public void ReadSectionValues(string Section, NameValueCollection Values)
bubuko.com,布布扣    {
bubuko.com,布布扣      StringCollection KeyList = new StringCollection();
bubuko.com,布布扣      ReadSection(Section, KeyList);
bubuko.com,布布扣      Values.Clear();
bubuko.com,布布扣      foreach (string key in KeyList)
bubuko.com,布布扣      {
bubuko.com,布布扣        Values.Add(key, ReadString(Section, key, ""));
bubuko.com,布布扣  
bubuko.com,布布扣      }
bubuko.com,布布扣    }
bubuko.com,布布扣    ////读取指定的Section的所有Value到列表中,
bubuko.com,布布扣    //public void ReadSectionValues(string Section, NameValueCollection Values,char splitString)
bubuko.com,布布扣    //{  string sectionValue;
bubuko.com,布布扣    //  string[] sectionValueSplit;
bubuko.com,布布扣    //  StringCollection KeyList = new StringCollection();
bubuko.com,布布扣    //  ReadSection(Section, KeyList);
bubuko.com,布布扣    //  Values.Clear();
bubuko.com,布布扣    //  foreach (string key in KeyList)
bubuko.com,布布扣    //  {
bubuko.com,布布扣    //    sectionValue=ReadString(Section, key, "");
bubuko.com,布布扣    //    sectionValueSplit=sectionValue.Split(splitString);
bubuko.com,布布扣    //    Values.Add(key, sectionValueSplit[0].ToString(),sectionValueSplit[1].ToString());
bubuko.com,布布扣 
bubuko.com,布布扣    //  }
bubuko.com,布布扣    //}
bubuko.com,布布扣    //清除某个Section
bubuko.com,布布扣    public void EraseSection(string Section)
bubuko.com,布布扣    {
bubuko.com,布布扣      //
bubuko.com,布布扣      if (!WritePrivateProfileString(Section, null, null, FileName))
bubuko.com,布布扣      {
bubuko.com,布布扣
bubuko.com,布布扣        throw (new ApplicationException("无法清除Ini文件中的Section"));
bubuko.com,布布扣      }
bubuko.com,布布扣    }
bubuko.com,布布扣    //删除某个Section下的键
bubuko.com,布布扣    public void DeleteKey(string Section, string Ident)
bubuko.com,布布扣    {
bubuko.com,布布扣      WritePrivateProfileString(Section, Ident, null, FileName);
bubuko.com,布布扣    }
bubuko.com,布布扣    //Note:对于Win9X,来说需要实现UpdateFile方法将缓冲中的数据写入文件
bubuko.com,布布扣    //在Win NT, 2000和XP上,都是直接写文件,没有缓冲,所以,无须实现UpdateFile
bubuko.com,布布扣    //执行完对Ini文件的修改之后,应该调用本方法更新缓冲区。
bubuko.com,布布扣    public void UpdateFile()
bubuko.com,布布扣    {
bubuko.com,布布扣      WritePrivateProfileString(null, null, null, FileName);
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    //检查某个Section下的某个键值是否存在
bubuko.com,布布扣    public bool ValueExists(string Section, string Ident)
bubuko.com,布布扣    {
bubuko.com,布布扣      //
bubuko.com,布布扣      StringCollection Idents = new StringCollection();
bubuko.com,布布扣      ReadSection(Section, Idents);
bubuko.com,布布扣      return Idents.IndexOf(Ident) > -1;
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    //确保资源的释放
bubuko.com,布布扣    ~IniFiles()
bubuko.com,布布扣    {
bubuko.com,布布扣      UpdateFile();
bubuko.com,布布扣    }
bubuko.com,布布扣  }
bubuko.com,布布扣}

C# 读写INI 文件

标签:style   blog   http   color   io   os   ar   strong   for   

原文地址:http://www.cnblogs.com/Futuer/p/3992645.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!