C#读写ini文件
1 using System; 2 using System.Collections.Generic; 3 using System.Runtime.InteropServices; 4 using System.Text; 5 6 namespace Unlock 7 { 8 class RWIni 9 { 10 public string Path { get; set; } 11 public RWIni(string path) 12 { 13 this.Path = path; 14 } 15 16 [DllImport("kernel32")] 17 private static extern int GetPrivateProfileString(string section, string key, string defVal, StringBuilder retVal, int size, string filePath); 18 19 /// <summary> 20 /// 读ini文件 21 /// </summary> 22 /// <param name="section"></param> 23 /// <param name="key"></param> 24 /// <returns></returns> 25 public string ReadIni(string section, string key) 26 { 27 StringBuilder temp = new StringBuilder(500); 28 int i= GetPrivateProfileString(section, key, "", temp, 500, this.Path); 29 return temp.ToString(); 30 } 31 32 [DllImport("kernel32")] 33 private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); 34 35 /// <summary> 36 /// 写ini文件 37 /// </summary> 38 /// <param name="section"></param> 39 /// <param name="key"></param> 40 /// <param name="value"></param> 41 /// <param name="path"></param> 42 public void WriteIni(string section, string key, string value) 43 { 44 WritePrivateProfileString(section, key, value, this.Path); 45 } 46 } 47 }