C# 读写Ini 文件

代码如下:

 

  1. # 读写 Ini 文件   
  2. using System.Runtime.InteropServices;  
  3. public class IniFile  
  4. {  
  5.     private string path;  
  6.  
  7.     public IniFile(string iniPath)  
  8.     {  
  9.         this.path = iniPath;  
  10.     }  
  11.  
  12.     [DllImport("kernel32")]  
  13.     private static extern int GetPrivateProfileString  
  14.         (string section, string key, string def, StringBuilder retVal, int size, string filePath);  
  15.     [DllImport("kernel32")]  
  16.     private static extern long WritePrivateProfileString  
  17.         (string section, string key, string val, string filePath);  
  18.  
  19.     public string IniReadValue(string section, string key)  
  20.     {  
  21.         try 
  22.         {  
  23.             StringBuilder retVal = new StringBuilder(0xff);  
  24.             if (GetPrivateProfileString(section, key, "", retVal, 0xff, this.path) == 0)  
  25.             {  
  26.                 return string.Empty;  
  27.             }  
  28.             return retVal.ToString();  
  29.         }  
  30.         catch (Exception exception)  
  31.         {  
  32.             return exception.ToString();  
  33.         }  
  34.     }  
  35.  
  36.     public void IniWriteValue(string Section, string Key, string Value)  
  37.     {  
  38.         WritePrivateProfileString(Section, Key, Value, this.path);  
  39.     }  

 

posted @ 2014-02-26 13:32  zzg168  阅读(98)  评论(0编辑  收藏  举报