| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
bdnp
10年前发布

C#进行INI文件的读写操作

VC中提供了API函数进行INI文件的读写操作,但是微软推出的C#编程语言中却没有相应的方法,下面是一个C# ini文件读写类,从网上收集的,很全,就是没有对section的改名功能。

using System;  using System.IO;  using System.Runtime.InteropServices;  using System.Text;  using System.Collections;  using System.Collections.Specialized;     namespace wuyisky{    /**//**/    /**//// <summary>    /// IniFiles的类    /// </summary>    public class IniFiles    {      public string FileName; //INI文件名      //声明读写INI文件的API函数      [DllImport("kernel32")]      private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath);      [DllImport("kernel32")]      private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);      //类的构造函数,传递INI文件名      public IniFiles(string AFileName)      {        // 判断文件是否存在        FileInfo fileInfo = new FileInfo(AFileName);        //Todo:搞清枚举的用法        if ((!fileInfo.Exists))        { //|| (FileAttributes.Directory in fileInfo.Attributes))          //文件不存在,建立文件          System.IO.StreamWriter sw = new System.IO.StreamWriter(AFileName, false, System.Text.Encoding.Default);          try          {            sw.Write("#表格配置档案");            sw.Close();          }             catch          {            throw (new ApplicationException("Ini文件不存在"));          }        }        //必须是完全路径,不能是相对路径        FileName = fileInfo.FullName;      }      //写INI文件      public void WriteString(string Section, string Ident, string Value)      {        if (!WritePrivateProfileString(Section, Ident, Value, FileName))        {             throw (new ApplicationException("写Ini文件出错"));        }      }      //读取INI文件指定      public string ReadString(string Section, string Ident, string Default)      {        Byte[] Buffer = new Byte[65535];        int bufLen = GetPrivateProfileString(Section, Ident, Default, Buffer, Buffer.GetUpperBound(0), FileName);        //必须设定0(系统默认的