C# ini文件操作帮助类

 1 public class IniConfigHelper
 2 {
 3 // 声明INI文件的写操作函数 WritePrivateProfileString()
 4 
 5         [System.Runtime.InteropServices.DllImport("kernel32")]
 6         private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
 7 
 8         // 声明INI文件的读操作函数 GetPrivateProfileString()
 9         [System.Runtime.InteropServices.DllImport("kernel32")]
10         private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath);
11 
12 
13 
14         private string sPath = null;
15 
16 
17         public IniConfigHelper(string path)
18         {
19             this.sPath = path;
20         }
21 
22         /// <summary>
23         ///  section=配置节,key=键名,value=键值,path=路径  
24         /// </summary>
25         /// <param name="section"></param>
26         /// <param name="key"></param>
27         /// <param name="value"></param>
28         public void Write(string section, string key, string value)
29         {
30             // section=配置节,key=键名,value=键值,path=路径  
31 
32             WritePrivateProfileString(section, key, value, sPath);
33         }
34         
35 
36         public string ReadValue(string section, string key)
37         {
38             // 每次从ini中读取多少字节
39             System.Text.StringBuilder temp = new System.Text.StringBuilder(255);
40             // section=配置节,key=键名,temp=上面,读取该键名的值最大长度,path=路径
41             GetPrivateProfileString(section, key, "", temp, 255, sPath);
42             return temp.ToString();
43         }
44 
45     }
原文地址:https://www.cnblogs.com/MichaelJson/p/12894812.html