| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 
 | #region INI 文本帮助类 API 声明
 
 
 
 
 
 
 [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
 private static extern uint GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer, uint nSize, string lpFileName);
 
 
 
 
 
 
 
 
 
 [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
 private static extern uint GetPrivateProfileSection(string lpAppName, IntPtr lpReturnedString, uint nSize, string lpFileName);
 
 
 
 
 
 
 
 
 
 
 
 [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
 private static extern uint GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, [In, Out] char[] lpReturnedString, uint nSize, string lpFileName);
 
 
 
 
 
 
 
 
 
 
 
 [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
 private static extern uint GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, uint nSize, string lpFileName);
 
 
 
 
 
 
 
 
 
 
 
 [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
 private static extern uint GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, string lpReturnedString, uint nSize, string lpFileName);
 
 
 
 
 
 
 
 
 [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
 private static extern bool WritePrivateProfileSection(string lpAppName, string lpString, string lpFileName);
 
 
 
 
 
 
 
 
 
 [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
 [return: MarshalAs(UnmanagedType.Bool)]
 private static extern bool WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName);
 #endregion
 
 #region INI 文本帮助类 封装方法
 
 
 
 
 
 public static string[] INIGetAllSectionNames(string iniFile)
 {
 
 uint MAX_BUFFER = 32767;
 
 string[] sections = new string[0];
 
 IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char));
 uint bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, iniFile);
 if (bytesReturned != 0)
 {
 
 string local = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned).ToString();
 
 sections = local.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
 }
 
 Marshal.FreeCoTaskMem(pReturnedString);
 return sections;
 }
 
 
 
 
 
 
 
 public static string[] INIGetAllItems(string iniFile, string section)
 {
 
 uint MAX_BUFFER = 32767;
 string[] items = new string[0];
 
 IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char));
 uint bytesReturned = GetPrivateProfileSection(section, pReturnedString, MAX_BUFFER, iniFile);
 if (!(bytesReturned == MAX_BUFFER - 2) || (bytesReturned == 0))
 {
 string returnedString = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned);
 items = returnedString.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
 }
 
 Marshal.FreeCoTaskMem(pReturnedString);
 return items;
 }
 
 
 
 
 
 
 
 public static string[] INIGetAllItemKeys(string iniFile, string section)
 {
 string[] value = new string[0];
 const int SIZE = 1024 * 10;
 if (string.IsNullOrEmpty(section))
 {
 throw new ArgumentException("必须指定节点名称", "section");
 }
 char[] chars = new char[SIZE];
 uint bytesReturned = GetPrivateProfileString(section, null, null, chars, SIZE, iniFile);
 if (bytesReturned != 0)
 {
 value = new string(chars).Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
 }
 chars = null;
 return value;
 }
 
 
 
 
 
 
 
 
 
 public static string INIGetStringValue(string iniFile, string section, string key, string defaultValue)
 {
 string value = defaultValue;
 const int SIZE = 1024 * 10;
 if (string.IsNullOrEmpty(section))
 {
 throw new ArgumentException("必须指定节点名称", "section");
 }
 if (string.IsNullOrEmpty(key))
 {
 throw new ArgumentException("必须指定键名称(key)", "key");
 }
 StringBuilder sb = new StringBuilder(SIZE);
 uint bytesReturned = GetPrivateProfileString(section, key, defaultValue, sb, SIZE, iniFile);
 if (bytesReturned != 0)
 {
 value = sb.ToString();
 }
 sb = null;
 return value;
 }
 
 
 
 
 
 
 
 
 public static bool INIWriteItems(string iniFile, string section, string items)
 {
 if (string.IsNullOrEmpty(section))
 {
 throw new ArgumentException("必须指定节点名称", "section");
 }
 if (string.IsNullOrEmpty(items))
 {
 throw new ArgumentException("必须指定键值对", "items");
 }
 return WritePrivateProfileSection(section, items, iniFile);
 }
 
 
 
 
 
 
 
 
 
 public static bool INIWriteValue(string iniFile, string section, string key, string value)
 {
 if (string.IsNullOrEmpty(section))
 {
 throw new ArgumentException("必须指定节点名称", "section");
 }
 if (string.IsNullOrEmpty(key))
 {
 throw new ArgumentException("必须指定键名称", "key");
 }
 if (value == null)
 {
 throw new ArgumentException("值不能为null", "value");
 }
 return WritePrivateProfileString(section, key, value, iniFile);
 }
 
 
 
 
 
 
 
 
 public static bool INIDeleteKey(string iniFile, string section, string key)
 {
 if (string.IsNullOrEmpty(section))
 {
 throw new ArgumentException("必须指定节点名称", "section");
 }
 if (string.IsNullOrEmpty(key))
 {
 throw new ArgumentException("必须指定键名称", "key");
 }
 return WritePrivateProfileString(section, key, null, iniFile);
 }
 
 
 
 
 
 
 
 public static bool INIDeleteSection(string iniFile, string section)
 {
 if (string.IsNullOrEmpty(section))
 {
 throw new ArgumentException("必须指定节点名称", "section");
 }
 return WritePrivateProfileString(section, null, null, iniFile);
 }
 
 
 
 
 
 
 
 public static bool INIEmptySection(string iniFile, string section)
 {
 if (string.IsNullOrEmpty(section))
 {
 throw new ArgumentException("必须指定节点名称", "section");
 }
 return WritePrivateProfileSection(section, string.Empty, iniFile);
 }
 #endregion
 
 |