ini配置文件读取类
![ContractedBlock.gif](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1
using
System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Runtime.InteropServices;
5 using System.IO;
6
7 namespace DABAOKU
8 {
9 /// <summary>
10 /// 读写ini配置文件类
11 /// </summary>
12 class IniFile
13 {
14 #region 成员变量
15 public string filePath = "" ; // 文件路径
16 static private uint MaxBufferSize = 32767 ; // 缓存大小
17 #endregion
18
19 #region 导入API
20 [DllImport( " kernel32 " , CharSet = CharSet.Auto)]
21 private static extern long WritePrivateProfileString( string section, string key, string val, string filePath);
22 [DllImport( " kernel32 " , CharSet = CharSet.Auto)]
23 private static extern int GetPrivateProfileString( string section, string key, string def, StringBuilder retVal, int size, string filePath);
24 [DllImport( " kernel32 " , CharSet = CharSet.Auto)]
25 private static extern int GetPrivateProfileString( string section, string key, string def, IntPtr retVal, int size, string filePath);
26 [DllImport( " kernel32 " , CharSet = CharSet.Auto)]
27 private static extern int GetPrivateProfileInt( string section, string key, int def, string filePath);
28 [DllImport( " kernel32 " , CharSet = CharSet.Auto)]
29 private static extern int GetPrivateProfileSection( string section, IntPtr lpReturnedString, uint nSize, string filePath);
30 [DllImport( " kernel32 " , CharSet = CharSet.Auto)]
31 private static extern int GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer, uint nSize, string filepath);
32 #endregion
33
34 #region 构造函数
35
36 public IniFile( string filename)
37 {
38 filePath = System.IO.Path.GetFullPath(filename);
39 }
40 #endregion
41
42 #region 读取数据
43
44 public string GetString( string Section, string Key)
45 {
46 StringBuilder temp = new StringBuilder( 255 );
47 int i = GetPrivateProfileString(Section, Key, "" , temp, 255 , this .filePath);
48 return temp.ToString();
49 }
50
51 public int GetInt( string Section, string Key)
52 {
53 return GetPrivateProfileInt(Section, Key, 0 , this .filePath);
54 }
55
56 public List < KeyValuePair < string , string >> GetValueSetList( string Section) // 读取一段内的所有数据
57 {
58 List < KeyValuePair < string , string >> retval;
59 string [] keyValuePairs;
60 string key, value;
61 int equalSignPos;
62 // 申请空间
63 IntPtr ptr = Marshal.AllocCoTaskMem(( int )IniFile.MaxBufferSize);
64
65 try
66 {
67 int len = GetPrivateProfileSection(Section, ptr, MaxBufferSize, filePath);
68 keyValuePairs = ConvertNullSeperatedStringToStringArray(ptr, len);
69 }
70 finally
71 {
72 Marshal.FreeCoTaskMem(ptr);
73 }
74 // 输出结果
75 retval = new List < KeyValuePair < string , string >> (keyValuePairs.Length);
76 for ( int i = 0 ; i < keyValuePairs.Length; ++ i)
77 {
78 equalSignPos = keyValuePairs[i].IndexOf( ' = ' );
79 key = keyValuePairs[i].Substring( 0 , equalSignPos);
80 value = keyValuePairs[i].Substring(equalSignPos + 1 , keyValuePairs[i].Length - equalSignPos - 1 );
81 retval.Add( new KeyValuePair < string , string > (key, value));
82 }
83 return retval;
84 }
85 public string [] GetSectionNames() // 获得所有段名
86 {
87 string [] retval;
88 int len;
89 IntPtr ptr = Marshal.AllocCoTaskMem(( int )IniFile.MaxBufferSize);
90 try
91 {
92 len = GetPrivateProfileSectionNames(ptr, IniFile.MaxBufferSize, filePath);
93 retval = ConvertNullSeperatedStringToStringArray(ptr, len);
94 }
95 finally
96 {
97 Marshal.FreeCoTaskMem(ptr);
98 }
99 return retval;
100 }
101 public string [] GetKeyNames( string Section) // 获得一段内所有关键码
102 {
103 int len;
104 string [] retval;
105 IntPtr ptr = Marshal.AllocCoTaskMem(( int )IniFile.MaxBufferSize);
106 try
107 {
108 len = GetPrivateProfileString(Section, null , null , ptr, ( int )IniFile.MaxBufferSize, filePath);
109 retval = ConvertNullSeperatedStringToStringArray(ptr, len);
110 }
111 finally
112 {
113 Marshal.FreeCoTaskMem(ptr);
114 }
115 return retval;
116 }
117
118 private static string [] ConvertNullSeperatedStringToStringArray(IntPtr ptr, int valLength) // 转换字符串指针空间为字符串数组
119 {
120 string [] retval;
121 if (valLength == 0 )
122 {
123 retval = new string [ 0 ];
124 }
125 else
126 {
127 string buff = Marshal.PtrToStringAuto(ptr, valLength - 1 );
128 retval = buff.Split( ' \0 ' );
129 }
130 return retval;
131 }
132 #endregion
133
134 #region 写数据
135
136 public void SetValue( string Section, string Key, string Value)
137 {
138 WritePrivateProfileString(Section, Key, Value, this .filePath);
139 }
140 public void SetValue( string Section, string Key, int Value)
141 {
142 WritePrivateProfileString(Section, Key, Value.ToString(), this .filePath);
143 }
144 public void SetValue( string Section, string Key, float Value)
145 {
146 WritePrivateProfileString(Section, Key, Value.ToString(), this .filePath);
147 }
148 public void SetValue( string Section, string Key, double Value)
149 {
150 WritePrivateProfileString(Section, Key, Value.ToString(), this .filePath);
151 }
152
153 #endregion
154
155 #region 删除
156 public void DeleteKey( string Section, string Key)
157 {
158 SetValue(Section, Key, null );
159 }
160 public void DeleteKey( string Section)
161 {
162 SetValue(Section, null , null );
163 }
164 #endregion
165
166 #region 补充
167 static public void FormatIni( string filePath)
168 {
169 StreamReader sr = new StreamReader(filePath);
170 StreamWriter sw = new StreamWriter(filePath + " .gc1 " );
171 string s = "" ;
172 while ((s = sr.ReadLine()) != null )
173 {
174 if (s != "" && s.IndexOf( " [ " ) == - 1 && s.IndexOf( " = " ) == - 1 )
175 s += " = " ;
176 sw.WriteLine(s);
177 }
178 sr.Close();
179 sw.Close();
180 try
181 {
182 File.Move(filePath, filePath + " .gc " );
183 }
184 catch (System.Exception ex){}
185 try
186 {
187 File.Move(filePath + " .gc1 " , filePath);
188 }
189 catch (System.Exception ex){}
190 try
191 {
192 File.Delete(filePath + " .gc1 " );
193 }
194 catch (System.Exception ex){}
195 }
196 #endregion
197 }
198 }
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Runtime.InteropServices;
5 using System.IO;
6
7 namespace DABAOKU
8 {
9 /// <summary>
10 /// 读写ini配置文件类
11 /// </summary>
12 class IniFile
13 {
14 #region 成员变量
15 public string filePath = "" ; // 文件路径
16 static private uint MaxBufferSize = 32767 ; // 缓存大小
17 #endregion
18
19 #region 导入API
20 [DllImport( " kernel32 " , CharSet = CharSet.Auto)]
21 private static extern long WritePrivateProfileString( string section, string key, string val, string filePath);
22 [DllImport( " kernel32 " , CharSet = CharSet.Auto)]
23 private static extern int GetPrivateProfileString( string section, string key, string def, StringBuilder retVal, int size, string filePath);
24 [DllImport( " kernel32 " , CharSet = CharSet.Auto)]
25 private static extern int GetPrivateProfileString( string section, string key, string def, IntPtr retVal, int size, string filePath);
26 [DllImport( " kernel32 " , CharSet = CharSet.Auto)]
27 private static extern int GetPrivateProfileInt( string section, string key, int def, string filePath);
28 [DllImport( " kernel32 " , CharSet = CharSet.Auto)]
29 private static extern int GetPrivateProfileSection( string section, IntPtr lpReturnedString, uint nSize, string filePath);
30 [DllImport( " kernel32 " , CharSet = CharSet.Auto)]
31 private static extern int GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer, uint nSize, string filepath);
32 #endregion
33
34 #region 构造函数
35
36 public IniFile( string filename)
37 {
38 filePath = System.IO.Path.GetFullPath(filename);
39 }
40 #endregion
41
42 #region 读取数据
43
44 public string GetString( string Section, string Key)
45 {
46 StringBuilder temp = new StringBuilder( 255 );
47 int i = GetPrivateProfileString(Section, Key, "" , temp, 255 , this .filePath);
48 return temp.ToString();
49 }
50
51 public int GetInt( string Section, string Key)
52 {
53 return GetPrivateProfileInt(Section, Key, 0 , this .filePath);
54 }
55
56 public List < KeyValuePair < string , string >> GetValueSetList( string Section) // 读取一段内的所有数据
57 {
58 List < KeyValuePair < string , string >> retval;
59 string [] keyValuePairs;
60 string key, value;
61 int equalSignPos;
62 // 申请空间
63 IntPtr ptr = Marshal.AllocCoTaskMem(( int )IniFile.MaxBufferSize);
64
65 try
66 {
67 int len = GetPrivateProfileSection(Section, ptr, MaxBufferSize, filePath);
68 keyValuePairs = ConvertNullSeperatedStringToStringArray(ptr, len);
69 }
70 finally
71 {
72 Marshal.FreeCoTaskMem(ptr);
73 }
74 // 输出结果
75 retval = new List < KeyValuePair < string , string >> (keyValuePairs.Length);
76 for ( int i = 0 ; i < keyValuePairs.Length; ++ i)
77 {
78 equalSignPos = keyValuePairs[i].IndexOf( ' = ' );
79 key = keyValuePairs[i].Substring( 0 , equalSignPos);
80 value = keyValuePairs[i].Substring(equalSignPos + 1 , keyValuePairs[i].Length - equalSignPos - 1 );
81 retval.Add( new KeyValuePair < string , string > (key, value));
82 }
83 return retval;
84 }
85 public string [] GetSectionNames() // 获得所有段名
86 {
87 string [] retval;
88 int len;
89 IntPtr ptr = Marshal.AllocCoTaskMem(( int )IniFile.MaxBufferSize);
90 try
91 {
92 len = GetPrivateProfileSectionNames(ptr, IniFile.MaxBufferSize, filePath);
93 retval = ConvertNullSeperatedStringToStringArray(ptr, len);
94 }
95 finally
96 {
97 Marshal.FreeCoTaskMem(ptr);
98 }
99 return retval;
100 }
101 public string [] GetKeyNames( string Section) // 获得一段内所有关键码
102 {
103 int len;
104 string [] retval;
105 IntPtr ptr = Marshal.AllocCoTaskMem(( int )IniFile.MaxBufferSize);
106 try
107 {
108 len = GetPrivateProfileString(Section, null , null , ptr, ( int )IniFile.MaxBufferSize, filePath);
109 retval = ConvertNullSeperatedStringToStringArray(ptr, len);
110 }
111 finally
112 {
113 Marshal.FreeCoTaskMem(ptr);
114 }
115 return retval;
116 }
117
118 private static string [] ConvertNullSeperatedStringToStringArray(IntPtr ptr, int valLength) // 转换字符串指针空间为字符串数组
119 {
120 string [] retval;
121 if (valLength == 0 )
122 {
123 retval = new string [ 0 ];
124 }
125 else
126 {
127 string buff = Marshal.PtrToStringAuto(ptr, valLength - 1 );
128 retval = buff.Split( ' \0 ' );
129 }
130 return retval;
131 }
132 #endregion
133
134 #region 写数据
135
136 public void SetValue( string Section, string Key, string Value)
137 {
138 WritePrivateProfileString(Section, Key, Value, this .filePath);
139 }
140 public void SetValue( string Section, string Key, int Value)
141 {
142 WritePrivateProfileString(Section, Key, Value.ToString(), this .filePath);
143 }
144 public void SetValue( string Section, string Key, float Value)
145 {
146 WritePrivateProfileString(Section, Key, Value.ToString(), this .filePath);
147 }
148 public void SetValue( string Section, string Key, double Value)
149 {
150 WritePrivateProfileString(Section, Key, Value.ToString(), this .filePath);
151 }
152
153 #endregion
154
155 #region 删除
156 public void DeleteKey( string Section, string Key)
157 {
158 SetValue(Section, Key, null );
159 }
160 public void DeleteKey( string Section)
161 {
162 SetValue(Section, null , null );
163 }
164 #endregion
165
166 #region 补充
167 static public void FormatIni( string filePath)
168 {
169 StreamReader sr = new StreamReader(filePath);
170 StreamWriter sw = new StreamWriter(filePath + " .gc1 " );
171 string s = "" ;
172 while ((s = sr.ReadLine()) != null )
173 {
174 if (s != "" && s.IndexOf( " [ " ) == - 1 && s.IndexOf( " = " ) == - 1 )
175 s += " = " ;
176 sw.WriteLine(s);
177 }
178 sr.Close();
179 sw.Close();
180 try
181 {
182 File.Move(filePath, filePath + " .gc " );
183 }
184 catch (System.Exception ex){}
185 try
186 {
187 File.Move(filePath + " .gc1 " , filePath);
188 }
189 catch (System.Exception ex){}
190 try
191 {
192 File.Delete(filePath + " .gc1 " );
193 }
194 catch (System.Exception ex){}
195 }
196 #endregion
197 }
198 }