一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - C# - C#操作INI文件的輔助類IniHelper

C#操作INI文件的輔助類IniHelper

2022-02-21 14:50馬洪彪 C#

這篇文章主要為大家詳細介紹了C#操作INI文件的輔助類IniHelper,具有一定的參考價值,感興趣的小伙伴們可以參考一下

使用INI配置文件,簡單便捷。

該輔助工具類為C#操作INI文件的輔助類,源碼在某位師傅的基礎上完善的來,因為忘記最初的來源了,因此不能提及引用,在此深感遺憾,并對貢獻者表示感謝。

?
1
2
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
 
namespace Eyuan.Common
{
 public static class INIHelper
 {
 
  #region 讀寫INI文件相關
  [DllImport("kernel32.dll", EntryPoint = "WritePrivateProfileString", CharSet = CharSet.Ansi)]
  private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
 
  [DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileString", CharSet = CharSet.Ansi)]
  private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
 
  [DllImport("kernel32")]
  private static extern int GetPrivateProfileInt(string lpApplicationName, string lpKeyName, int nDefault, string lpFileName);
 
 
  [DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileSectionNames", CharSet = CharSet.Ansi)]
  private static extern int GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer, int nSize, string filePath);
 
  [DllImport("KERNEL32.DLL ", EntryPoint = "GetPrivateProfileSection", CharSet = CharSet.Ansi)]
  private static extern int GetPrivateProfileSection(string lpAppName, byte[] lpReturnedString, int nSize, string filePath);
  #endregion
 
  #region 讀寫操作(字符串)
  /// <summary>
  /// 向INI寫入數據
  /// </summary>
  /// <PARAM name="Section">節點名</PARAM>
  /// <PARAM name="Key">鍵名</PARAM>
  /// <PARAM name="Value">值(字符串)</PARAM>
  public static void Write(string Section, string Key, string Value, string path)
  {
   WritePrivateProfileString(Section, Key, Value, path);
  }
  /// <summary>
  /// 讀取INI數據
  /// </summary>
  /// <PARAM name="Section">節點名</PARAM>
  /// <PARAM name="Key">鍵名</PARAM>
  /// <PARAM name="Path">值名</PARAM>
  /// <returns>值(字符串)</returns>
  public static string Read(string Section, string Key, string path)
  {
   StringBuilder temp = new StringBuilder(255);
   int i = GetPrivateProfileString(Section, Key, "", temp, 255, path);
   return temp.ToString();
  }
  #endregion
 
  #region 配置節信息
  /// <summary>
  /// 讀取一個ini里面所有的節
  /// </summary>
  /// <param name="sections"></param>
  /// <param name="path"></param>
  /// <returns>-1:沒有節信息,0:正常</returns>
  public static int GetAllSectionNames(out string[] sections, string path)
  {
   int MAX_BUFFER = 32767;
   IntPtr pReturnedString = Marshal.AllocCoTaskMem(MAX_BUFFER);
   int bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, path);
   if (bytesReturned == 0)
   {
    sections = null;
    return -1;
   }
   string local = Marshal.PtrToStringAnsi(pReturnedString, (int)bytesReturned).ToString();
   Marshal.FreeCoTaskMem(pReturnedString);
   //use of Substring below removes terminating null for split
   sections = local.Substring(0, local.Length - 1).Split('\0');
   return 0;
  }
  /// <summary>
  /// 返回指定配置文件下的節名稱列表
  /// </summary>
  /// <param name="path"></param>
  /// <returns></returns>
  public static List<string> GetAllSectionNames(string path)
  {
   List<string> sectionList = new List<string>();
   int MAX_BUFFER = 32767;
   IntPtr pReturnedString = Marshal.AllocCoTaskMem(MAX_BUFFER);
   int bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, path);
   if (bytesReturned != 0)
   {
    string local = Marshal.PtrToStringAnsi(pReturnedString, (int)bytesReturned).ToString();
    Marshal.FreeCoTaskMem(pReturnedString);
    sectionList.AddRange(local.Substring(0, local.Length - 1).Split('\0'));
   }
   return sectionList;
  }
 
  /// <summary>
  /// 得到某個節點下面所有的key和value組合
  /// </summary>
  /// <param name="section">指定的節名稱</param>
  /// <param name="keys">Key數組</param>
  /// <param name="values">Value數組</param>
  /// <param name="path">INI文件路徑</param>
  /// <returns></returns>
  public static int GetAllKeyValues(string section, out string[] keys, out string[] values, string path)
  {
   byte[] b = new byte[65535];//配置節下的所有信息
   GetPrivateProfileSection(section, b, b.Length, path);
   string s = System.Text.Encoding.Default.GetString(b);//配置信息
   string[] tmp = s.Split((char)0);//Key\Value信息
   List<string> result = new List<string>();
   foreach (string r in tmp)
   {
    if (r != string.Empty)
     result.Add(r);
   }
   keys = new string[result.Count];
   values = new string[result.Count];
   for (int i = 0; i < result.Count; i++)
   {
    string[] item = result[i].Split(new char[] { '=' });//Key=Value格式的配置信息
    //Value字符串中含有=的處理,
    //一、Value加"",先對""處理
    //二、Key后續的都為Value
    if (item.Length > 2)
    {
     keys[i] = item[0].Trim();
     values[i] = result[i].Substring(keys[i].Length + 1);
    }
    if (item.Length == 2)//Key=Value
    {
     keys[i] = item[0].Trim();
     values[i] = item[1].Trim();
    }
    else if (item.Length == 1)//Key=
    {
     keys[i] = item[0].Trim();
     values[i] = "";
    }
    else if (item.Length == 0)
    {
     keys[i] = "";
     values[i] = "";
    }
   }
   return 0;
  }
  /// <summary>
  /// 得到某個節點下面所有的key
  /// </summary>
  /// <param name="section">指定的節名稱</param>
  /// <param name="keys">Key數組</param>
  /// <param name="path">INI文件路徑</param>
  /// <returns></returns>
  public static int GetAllKeys(string section, out string[] keys, string path)
  {
   byte[] b = new byte[65535];
 
   GetPrivateProfileSection(section, b, b.Length, path);
   string s = System.Text.Encoding.Default.GetString(b);
   string[] tmp = s.Split((char)0);
   ArrayList result = new ArrayList();
   foreach (string r in tmp)
   {
    if (r != string.Empty)
     result.Add(r);
   }
   keys = new string[result.Count];
   for (int i = 0; i < result.Count; i++)
   {
    string[] item = result[i].ToString().Split(new char[] { '=' });
    if (item.Length == 2)
    {
     keys[i] = item[0].Trim();
    }
    else if (item.Length == 1)
    {
     keys[i] = item[0].Trim();
    }
    else if (item.Length == 0)
    {
     keys[i] = "";
    }
   }
   return 0;
  }
  /// <summary>
  /// 獲取指定節下的Key列表
  /// </summary>
  /// <param name="section">指定的節名稱</param>
  /// <param name="path">配置文件名稱</param>
  /// <returns>Key列表</returns>
  public static List<string> GetAllKeys(string section, string path)
  {
   List<string> keyList = new List<string>();
   byte[] b = new byte[65535];
   GetPrivateProfileSection(section, b, b.Length, path);
   string s = System.Text.Encoding.Default.GetString(b);
   string[] tmp = s.Split((char)0);
   List<string> result = new List<string>();
   foreach (string r in tmp)
   {
    if (r != string.Empty)
     result.Add(r);
   }
   for (int i = 0; i < result.Count; i++)
   {
    string[] item = result[i].Split(new char[] { '=' });
    if (item.Length == 2 || item.Length == 1)
    {
     keyList.Add(item[0].Trim());
    }
    else if (item.Length == 0)
    {
     keyList.Add(string.Empty);
    }
   }
   return keyList;
  }
  /// <summary>
  /// 獲取值
  /// </summary>
  /// <param name="section"></param>
  /// <param name="path"></param>
  /// <returns></returns>
  public static List<string> GetAllValues(string section, string path)
  {
   List<string> keyList = new List<string>();
   byte[] b = new byte[65535];
   GetPrivateProfileSection(section, b, b.Length, path);
   string s = System.Text.Encoding.Default.GetString(b);
   string[] tmp = s.Split((char)0);
   List<string> result = new List<string>();
   foreach (string r in tmp)
   {
    if (r != string.Empty)
     result.Add(r);
   }
   for (int i = 0; i < result.Count; i++)
   {
    string[] item = result[i].Split(new char[] { '=' });
    if (item.Length == 2 || item.Length == 1)
    {
     keyList.Add(item[1].Trim());
    }
    else if (item.Length == 0)
    {
     keyList.Add(string.Empty);
    }
   }
   return keyList;
  }
  
  #endregion
 
  #region 通過值查找鍵(一個節中的鍵唯一,可能存在多個鍵值相同,因此反查的結果可能為多個)
 
  /// <summary>
  /// 第一個鍵
  /// </summary>
  /// <param name="section"></param>
  /// <param name="path"></param>
  /// <param name="value"></param>
  /// <returns></returns>
  public static string GetFirstKeyByValue(string section, string path, string value)
  {
   foreach (string key in GetAllKeys(section, path))
   {
    if (ReadString(section, key, "", path) == value)
    {
     return key;
    }
   }
   return string.Empty;
  }
  /// <summary>
  /// 所有鍵
  /// </summary>
  /// <param name="section"></param>
  /// <param name="path"></param>
  /// <param name="value"></param>
  /// <returns></returns>
  public static List<string> GetKeysByValue(string section, string path, string value)
  {
   List<string > keys = new List<string>();
   foreach (string key in GetAllKeys(section, path))
   {
    if (ReadString(section, key, "", path) == value)
    {
     keys.Add(key);
    }
   }
   return keys;
  }
  #endregion
 
 
  #region 具體類型的讀寫
 
  #region string
  /// <summary>
  ///
  /// </summary>
  /// <param name="sectionName"></param>
  /// <param name="keyName"></param>
  /// <param name="defaultValue" />
  /// <param name="path"></param>
  /// <returns></returns>
  public static string ReadString(string sectionName, string keyName, string defaultValue, string path)
  {
   const int MAXSIZE = 255;
   StringBuilder temp = new StringBuilder(MAXSIZE);
   GetPrivateProfileString(sectionName, keyName, defaultValue, temp, 255, path);
   return temp.ToString();
  }
 
  /// <summary>
  ///
  /// </summary>
  /// <param name="sectionName"></param>
  /// <param name="keyName"></param>
  /// <param name="value"></param>
  /// <param name="path"></param>
  public static void WriteString(string sectionName, string keyName, string value, string path)
  {
   WritePrivateProfileString(sectionName, keyName, value, path);
  }
  #endregion
 
  #region Int
  /// <summary>
  ///
  /// </summary>
  /// <param name="sectionName"></param>
  /// <param name="keyName"></param>
  /// <param name="defaultValue"></param>
  /// <param name="path"></param>
  /// <returns></returns>
  public static int ReadInteger(string sectionName, string keyName, int defaultValue, string path)
  {
 
   return GetPrivateProfileInt(sectionName, keyName, defaultValue, path);
 
  }
  /// <summary>
  ///
  /// </summary>
  /// <param name="sectionName"></param>
  /// <param name="keyName"></param>
  /// <param name="value"></param>
  /// <param name="path"></param>
  public static void WriteInteger(string sectionName, string keyName, int value, string path)
  {
 
   WritePrivateProfileString(sectionName, keyName, value.ToString(), path);
 
  }
  #endregion
 
  #region bool
  /// <summary>
  /// 讀取布爾值
  /// </summary>
  /// <param name="sectionName"></param>
  /// <param name="keyName"></param>
  /// <param name="defaultValue"></param>
  /// <param name="path"></param>
  /// <returns></returns>
  public static bool ReadBoolean(string sectionName, string keyName, bool defaultValue, string path)
  {
 
   int temp = defaultValue ? 1 : 0;
 
   int result = GetPrivateProfileInt(sectionName, keyName, temp, path);
 
   return (result == 0 ? false : true);
 
  }
  /// <summary>
  /// 寫入布爾值
  /// </summary>
  /// <param name="sectionName"></param>
  /// <param name="keyName"></param>
  /// <param name="value"></param>
  /// <param name="path"></param>
  public static void WriteBoolean(string sectionName, string keyName, bool value, string path)
  {
   string temp = value ? "1 " : "0 ";
   WritePrivateProfileString(sectionName, keyName, temp, path);
  }
  #endregion
 
  #endregion
 
  #region 刪除操作
  /// <summary>
  /// 刪除指定項
  /// </summary>
  /// <param name="sectionName"></param>
  /// <param name="keyName"></param>
  /// <param name="path"></param>
  public static void DeleteKey(string sectionName, string keyName, string path)
  {
   WritePrivateProfileString(sectionName, keyName, null, path);
  }
 
  /// <summary>
  /// 刪除指定節下的所有項
  /// </summary>
  /// <param name="sectionName"></param>
  /// <param name="path"></param>
  public static void EraseSection(string sectionName, string path)
  {
   WritePrivateProfileString(sectionName, null, null, path);
  }
  #endregion
 
  #region 判斷節、鍵是否存在
  /// <summary>
  /// 指定節知否存在
  /// </summary>
  /// <param name="section"></param>
  /// <param name="fileName"></param>
  /// <returns></returns>
  public static bool ExistSection(string section, string fileName)
  {
   string[] sections = null;
   GetAllSectionNames(out sections, fileName);
   if (sections != null)
   {
    foreach (var s in sections)
    {
     if (s == section)
     {
      return true;
     }
    }
   }
   return false;
  }
  /// <summary>
  /// 指定節下的鍵是否存在
  /// </summary>
  /// <param name="section"></param>
  /// <param name="key"></param>
  /// <param name="fileName"></param>
  /// <returns></returns>
  public static bool ExistKey(string section, string key, string fileName)
  {
   string[] keys = null;
   GetAllKeys(section, out keys, fileName);
   if (keys != null)
   {
    foreach (var s in keys)
    {
     if (s == key)
     {
      return true;
     }
    }
   }
   return false;
  }
  #endregion
 
  #region 同一Section下添加多個Key\Value
  /// <summary>
  ///
  /// </summary>
  /// <param name="section"></param>
  /// <param name="keyList"></param>
  /// <param name="valueList"></param>
  /// <param name="path"></param>
  /// <returns></returns>
  public static bool AddSectionWithKeyValues(string section, List<string> keyList, List<string> valueList, string path)
  {
   bool bRst = true;
   //判斷Section是否已經存在,如果存在,返回false
   //已經存在,則更新
   //if (GetAllSectionNames(path).Contains(section))
   //{
   // return false;
   //}
   //判斷keyList中是否有相同的Key,如果有,返回false
 
   //添加配置信息
   for (int i = 0; i < keyList.Count; i++)
   {
    WriteString(section, keyList[i], valueList[i], path);
   }
   return bRst;
  }
  #endregion
 }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://www.cnblogs.com/mahongbiao/p/3751153.html

延伸 · 閱讀

精彩推薦
  • C#WPF 自定義雷達圖開發實例教程

    WPF 自定義雷達圖開發實例教程

    這篇文章主要介紹了WPF 自定義雷達圖開發實例教程,本文介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下...

    WinterFish13112021-12-06
  • C#深入解析C#中的交錯數組與隱式類型的數組

    深入解析C#中的交錯數組與隱式類型的數組

    這篇文章主要介紹了深入解析C#中的交錯數組與隱式類型的數組,隱式類型的數組通常與匿名類型以及對象初始值設定項和集合初始值設定項一起使用,需要的...

    C#教程網6172021-11-09
  • C#Unity3D實現虛擬按鈕控制人物移動效果

    Unity3D實現虛擬按鈕控制人物移動效果

    這篇文章主要為大家詳細介紹了Unity3D實現虛擬按鈕控制人物移動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一...

    shenqingyu060520232410972022-03-11
  • C#C#實現XML文件讀取

    C#實現XML文件讀取

    這篇文章主要為大家詳細介紹了C#實現XML文件讀取的相關代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    Just_for_Myself6702022-02-22
  • C#C#裁剪,縮放,清晰度,水印處理操作示例

    C#裁剪,縮放,清晰度,水印處理操作示例

    這篇文章主要為大家詳細介紹了C#裁剪,縮放,清晰度,水印處理操作示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    吳 劍8332021-12-08
  • C#C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    這篇文章主要介紹了C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題,簡單描述了訪問者模式的定義并結合具體實例形式分析了C#使用訪問者模式解決長...

    GhostRider9502022-01-21
  • C#C#通過KD樹進行距離最近點的查找

    C#通過KD樹進行距離最近點的查找

    這篇文章主要為大家詳細介紹了C#通過KD樹進行距離最近點的查找,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    帆帆帆6112022-01-22
  • C#C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    這篇文章主要介紹了C# 實現對PPT文檔加密、解密及重置密碼的操作方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下...

    E-iceblue5012022-02-12
主站蜘蛛池模板: 日本中文字幕二区三区 | 韩国最新三级网站在线播放 | 日本丰满www色 | 973影院| 日本小视频网站 | 国内亚州视频在线观看 | 亚洲精品中文字幕在线 | 全肉np巨肉一女np高h双龙 | 青青草国产精品久久久久 | 亚州笫一色惰网站 | 经典三级四虎在线观看 | 国产免费精彩视频 | 成人在线观看免费视频 | 91无套极品外围在线播放 | 任我行视频在线观看国语 | 色吧欧美| 精品suv一区二区三区 | 人人揉揉香蕉 | 久久国产精品人妻中文 | blacked黑人hd2021| 天天天天天天天操 | yellow高清免费观看日本 | 国产日产韩产麻豆1区 | 色婷婷久久综合中文久久一本 | 91精品手机国产露脸 | 99任你躁精品视频 | 小黄文污到你湿 | 天堂男人在线 | 高清女主播一区二区三区 | 天堂中文在线观看 | 韩国三级做爰 | 亚洲午夜精品久久久久 | 欧美视频精品一区二区三区 | 色综合天天综合 | 免费一级特黄特色大片 | 我不卡影院手机在线观看 | 喜爱夜蒲2三级做爰 | 国产美女极品免费视频 | 跪趴好紧h| 国内精品免费一区二区三区 | 国产成人一区二区三区小说 |