INI文件是一种按照特点方式排列的文本文件。每一个INI文件构成都非常类似,由若干段落(section)组成,在每个带括号的标题下面,是若干个以单个单词开头的关键词(keyword)和一个等号,等
号右边的就是关键字对应的值(value)。其一般形式如下:
1 [section1] 2 keyword1=value1 3 keyword2=value2 4 keyword3=value3 5 [section2] 6 keyword1=value1 7 keyword2=value2
1 class Program 2 3 { 4 5 [DllImport("kernel32")] 6 7 private static extern long WritePrivateProfileString (string section ,string key , string val ,string filepath); 8 9 //参数说明:section:INI文件中的段落;key:INI文件中的关键字;val:INI文件中关键字的数值;filePath:INI文件的完整的路径和名称。 10 11 [DllImport ("kernel32")] 12 13 private static extern int GetPrivateProfileString (string section , string key , string def , StringBuilder retVal , int size , string filePath ) ; 14 15 //参数说明:section:INI文件中的段落名称;key:INI文件中的关键字;def:无法读取时候时候的缺省数值;retVal:读取数值;size:数值的大小;filePath:INI文件的完整路径和名称。 16 17 public static void Main(string[] args) 18 19 { 20 21 22 23 string section="database"; 24 25 string key="sql"; 26 27 //string value="server.;database=pubs;uid=sa;pwd="; 28 29 string fileName="d:\config.ini"; 30 31 32 33 try 34 35 { 36 37 //写入ini节点 38 39 //WritePrivateProfileString(section,key,value,fileName); 40 41 42 43 //读取ini节点 44 45 StringBuilder sb=new StringBuilder(255); 46 47 GetPrivateProfileString(section,key,"无法读取",sb,255,fileName); 48 49 50 51 Console.WriteLine(sb.ToString()); 52 53 54 55 }catch(Exception ex) 56 57 { 58 59 Console.WriteLine(ex.ToString()); 60 61 } 62 63 64 65 Console.Write("Press any key to continue . . . "); 66 67 Console.ReadKey(true); 68 69 } 70 71 72 73 }
整理出ini文件操作类:
1 /// <summary> 2 /// 读写INI文件的类。 3 /// </summary> 4 public class INIHelper 5 { 6 // 读写INI文件相关。 7 [DllImport("kernel32.dll", EntryPoint = "WritePrivateProfileString", CharSet = CharSet.Ansi)] 8 public static extern long WritePrivateProfileString(string section, string key, string val, string filePath); 9 [DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileString", CharSet = CharSet.Ansi)] 10 public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); 11 12 [DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileSectionNames", CharSet = CharSet.Ansi)] 13 public static extern int GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer, int nSize, string filePath); 14 15 [DllImport("KERNEL32.DLL ", EntryPoint = "GetPrivateProfileSection", CharSet = CharSet.Ansi)] 16 public static extern int GetPrivateProfileSection(string lpAppName, byte[] lpReturnedString, int nSize, string filePath); 17 18 19 /// <summary> 20 /// 向INI写入数据。 21 /// </summary> 22 /// <PARAM name="Section">节点名。</PARAM> 23 /// <PARAM name="Key">键名。</PARAM> 24 /// <PARAM name="Value">值名。</PARAM> 25 public static void Write(string Section, string Key, string Value, string path) 26 { 27 WritePrivateProfileString(Section, Key, Value, path); 28 } 29 30 31 /// <summary> 32 /// 读取INI数据。 33 /// </summary> 34 /// <PARAM name="Section">节点名。</PARAM> 35 /// <PARAM name="Key">键名。</PARAM> 36 /// <PARAM name="Path">值名。</PARAM> 37 /// <returns>相应的值。</returns> 38 public static string Read(string Section, string Key, string path) 39 { 40 StringBuilder temp = new StringBuilder(255); 41 int i = GetPrivateProfileString(Section, Key, "", temp, 255, path); 42 return temp.ToString(); 43 } 44 45 /// <summary> 46 /// 读取一个ini里面所有的节 47 /// </summary> 48 /// <param name="sections"></param> 49 /// <param name="path"></param> 50 /// <returns></returns> 51 public static int GetAllSectionNames(out string[] sections, string path) 52 { 53 int MAX_BUFFER = 32767; 54 IntPtr pReturnedString = Marshal.AllocCoTaskMem(MAX_BUFFER); 55 int bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, path); 56 if (bytesReturned == 0) 57 { 58 sections = null; 59 return -1; 60 } 61 string local = Marshal.PtrToStringAnsi(pReturnedString, (int)bytesReturned).ToString(); 62 Marshal.FreeCoTaskMem(pReturnedString); 63 //use of Substring below removes terminating null for split 64 sections = local.Substring(0, local.Length - 1).Split('