c#读取ini文件

 IniFile.cs
=============
using  System.Runtime.InteropServices;
using  System.Text;  
namespace  INIFile
{
    ///  
    ///  读写ini文件的类
    ///  调用kernel32.dll中的两个api:WritePrivateProfileString,GetPrivateProfileString来实现对ini  文件的读写。
    ///
    ///  INI文件是文本文件,
    ///  由若干节(section)组成,
    ///  在每个带括号的标题下面,
    ///  是若干个关键词(key)及其对应的值(value)
    ///  
  ///[Section]
  ///Key=value
    ///
    ///  

    public  class  IniFile

    {
        ///  
        ///  ini文件名称(带路径)
        ///  

        public  string  filePath;  

        //声明读写INI文件的API函数
        [DllImport("kernel32")]  
        private  static  extern  long  WritePrivateProfileString(string  section,string  key,string  val,string  filePath);

        [DllImport("kernel32")]
        private  static  extern  int  GetPrivateProfileString(string  section,string  key,string  def,StringBuilder  retVal,int  size,string  filePath);

        ///  
        ///  类的构造函数
        ///  

        ///  INI文件名  
        public  IniFile(string  INIPath)    
        {  
            filePath  =  INIPath;

        }

        ///  
        ///   写INI文件
        ///  

        ///  Section
        ///  Key
        ///  value
        public  void  WriteInivalue(string  Section,string  Key,string  value)    
        {      
            WritePrivateProfileString(Section,Key,value,this.filePath);

        }

        ///  
        ///    读取INI文件指定部分
        ///  

        ///  Section
        ///  Key
        ///  String  
        public  string  ReadInivalue(string  Section,string  Key)  
        {    
            StringBuilder  temp  =  new  StringBuilder(1024);
            int  i  =  GetPrivateProfileString(Section,Key,"读取错误",temp,1024,this.filePath);  
            return  temp.ToString();
        }  
    }  
}

应用程序
======
using INIFile;
NIFile.IniFile ini = new IniFile(Application.StartupPath+"//BankSet.ini");
string time = ini.ReadInivalue("Config","Time");

你可能感兴趣的:(c#读取ini文件)