https://www.cnblogs.com/ps3online/archive/2020/02/28/INIFile.htmlhtml
做者: 魔法軟糖node
日期: 2020-02-27windows
引言ide
*************************************ui
.ini 文件是Initialization File的縮寫,即配置文件 。是windows的系統配置文件所採用的存儲格式。編碼
它具備方便易用的特色,和註冊表的鍵值有着相似的功能,各種應用程序也常用INI保存各類配置和選項。spa
在簡單隻須要讀取的場合,調用WINDOWS API就行,但在複雜的須要可視化編輯時,就須要創建本身的處理類了。3d
如何實現本身的INI類調試
首先咱們須要瞭解code
◇ INI的格式
◇ 典型INI文件
;項目註釋
[.ShellClassInfo]
InfoTip=有圖標的文件夾
;圖標資源
IconResource="C:\Windows\system32\SHELL32.dll",4
#文件夾視圖
[ViewState]
Mode=
Vid=
FolderType=General
#尾部註釋
一個典型INI文件由節、註釋和節下面的項組成,而項爲鍵=值的形式。
INI文件的註釋符號有兩種,規範爲;分號,實際有些地方用#井號。
◇ 保留註釋
爲了在修改INI文件時不丟失任何信息,因此須要保存INI文件中全部有效元素、包括註釋甚至是無效行。
爲了實現這個目的,將全部註釋和無效行都歸屬於它以後的有效元素。
以上面的desktop.ini爲例,
◇ INIItem類
表示INI文件中節點下面的項,它擁有三個屬性:名稱Name、值Value和註釋Comment
1 /// <summary> 2 /// 擁有名稱、值和註釋 3 /// </summary> 4 public class INIItem { 5 /// <summary> 6 /// 實例化INIItem。指定名稱、值和註釋。 7 /// </summary> 8 /// <param name="vName"></param> 9 /// <param name="vValue"></param> 10 /// <param name="vComment"></param> 11 public INIItem(string vName, string vValue, string vComment = "") { 12 Name = vName; 13 Value = vValue; 14 Comment = vComment; 15 } 16 /// <summary> 17 /// 項名稱。例如 Color = 202,104,0 中的 Color 18 /// </summary> 19 public string Name { get; set; } 20 /// <summary> 21 /// 值內容。例如 Color = 202,104,0 中的 202,104,0 22 /// </summary> 23 public string Value { get; set; } 24 /// <summary> 25 /// 位於前面的全部註釋行。通常以 ; 開頭 26 /// </summary> 27 public string Comment { get; set; } 28 /// <summary> 29 /// 返回 INIItem 的文本形式。〈<see cref="string"/>〉 30 /// <para>Name=Value</para> 31 /// </summary> 32 /// <returns>〈string〉返回 INIItem 的文本形式。</returns> 33 public override string ToString() { 34 return Name + INI.U等號 + Value; 35 } 36 }
◇ ININode類
表示INI文件中的一個節點,它擁有項列表List{Of INIItem}、名稱Name和註釋Comment。
1 /// <summary> 2 /// 表示INI文件的一個節點,它擁有一個項目列表,還擁有名稱和註釋 3 /// <para></para> 4 /// </summary> 5 public class ININode { 6 /// <summary> 7 /// 實例化ININode。指定初始的名稱和註釋。 8 /// </summary> 9 /// <param name="vName"></param> 10 /// <param name="vComment"></param> 11 public ININode(string vName, string vComment) { Name = vName; Comment = vComment; Items = new List<INIItem>(); } 12 /// <summary> 13 /// 節點名稱。例如 [Config] 14 /// </summary> 15 public string Name { get; set; } 16 /// <summary> 17 /// 位於前面的全部註釋行。通常以 ; 開頭 18 /// </summary> 19 public string Comment { get; set; } 20 /// <summary> 21 /// 含有的項列表 22 /// </summary> 23 public List<INIItem> Items { get; set; } 24 /// <summary> 25 /// 向本節點添加新項。 26 /// </summary> 27 /// <param name="vName"></param> 28 /// <param name="vValue"></param> 29 /// <param name="vComment"></param> 30 /// <returns></returns> 31 public INIItem New(string vName, string vValue, string vComment = "") { 32 var k = new INIItem(vName, vValue, vComment); 33 Items.Add(k); 34 return k; 35 } 36 /// <summary> 37 /// 返回 ININode的文本形式。〈<see cref="string"/>〉 38 /// <para>[Name]</para> 39 /// </summary> 40 /// <returns>〈string〉返回 ININode 的文本形式。</returns> 41 public override string ToString() { 42 return INI.U左括號 + Name + INI.U右括號; 43 } 44 }
◇ INI類
它表示整個INI文件的所有內,擁有List{Of ININode}、EndText、FileName、StartLine等屬性
1 /// <summary> 2 /// 表示INI文件。擁有讀取和寫入文件的方法。 3 /// <para>儲存在 <see cref="List{ININode}"/><<see cref="ININode"/>></para> 4 /// </summary> 5 public class INI { 6 /// <summary> 7 /// 實例化INI文件。 8 /// </summary> 9 public INI() { } 10 11 #region "↓全局常量" 12 /// <summary>註釋的標準符號</summary> 13 public static string U註釋 = ";"; 14 /// <summary>註釋的標準符號2</summary> 15 public static string U註釋2 = "#"; 16 /// <summary>節左括號的標準符號</summary> 17 public static string U左括號 = "["; 18 /// <summary>節右括號的標準符號</summary> 19 public static string U右括號 = "]"; 20 /// <summary>鏈接項和值的標準符號</summary> 21 public static string U等號 = "="; 22 /// <summary>讀取或寫入時忽略無心義的備註行(不包括註釋)。</summary> 23 public static bool 忽略備註 = false; 24 /// <summary>讀取的上個文件的有效行數(不包括註釋)。</summary> 25 public static int 上次讀取的有效行數 = 0; 26 #endregion 27 28 /// <summary> 29 /// 全部節點 30 /// <para>每一個節點含有項、值和註釋,當項名稱爲空字符串時,整條語句視爲註釋</para> 31 /// </summary> 32 public List<ININode> Nodes { get; set; } = new List<ININode>(); 33 /// <summary> 34 /// 附加在INI文件後無心義的文本 35 /// </summary> 36 public string EndText { get; set; } = ""; 37 /// <summary> 38 /// 附加在INI文件第一行的做者信息等文本 39 /// <para>其中的換行符將被替換爲兩個空格</para> 40 /// </summary> 41 public string StartLine { get; set; } = ""; 42 /// <summary> 43 /// 讀取INI時得到的FileName。 44 /// <para>寫入文檔時可使用這個名字,也能夠不使用這個名字。</para> 45 /// </summary> 46 public string FileName { get; set; } = ""; 47 /// <summary> 48 /// 向本INI文件添加新節點。 49 /// </summary> 50 /// <param name="vName"></param> 51 /// <param name="vComment"></param> 52 /// <returns></returns> 53 public ININode New(string vName, string vComment = "") { 54 var k = new ININode(vName, vComment); 55 Nodes.Add(k); 56 return k; 57 } 58 }
如何寫入INI文件
如下是寫入代碼
1 #region "寫入文件" 2 3 /// <summary>將文檔寫入指定路徑 4 /// </summary> 5 /// <param name="path">指定路徑</param> 6 public bool 寫入文檔(string path, Encoding encoding = null) { 7 try { 8 if (encoding == null) { encoding = Encoding.Default; } 9 using (StreamWriter SW = new StreamWriter(path)) { 10 SW.Write(ToString()); 11 } 12 } catch (Exception) { 13 return false; 14 } 15 return true; 16 } 17 /// <summary> 18 /// 將INI文檔轉化爲文本格式,會生成整個文檔。 19 /// <para>注意:較大的文檔可能會耗費大量時間</para> 20 /// </summary> 21 /// <returns></returns> 22 public override string ToString() { 23 StringBuilder sb = new StringBuilder(); 24 if (StartLine.Length > 0) { sb.AppendLine(StartLine.Replace("\r\n", " ")); } 25 for (int i = 0; i < Nodes.Count; i++) { 26 var node = Nodes[i]; 27 if (忽略備註 == false) { sb.Append(node.Comment); } 28 sb.AppendLine(node.ToString()); 29 for (int j = 0; j < node.Items.Count; j++) { 30 var item = node.Items[j]; 31 if (忽略備註 == false) { sb.Append(item.Comment); } 32 sb.AppendLine(item.ToString()); 33 } 34 } 35 if (EndText.Length > 0) { sb.AppendLine(EndText); } 36 return sb.ToString(); 37 } 38 39 #endregion
如何讀取INI文件
讀取一般比寫入複雜。軟糖的代碼也是逐行檢查,屢次調試才完成。
流程以下:
代碼
#region "讀取文件" /// <summary> /// 從指定路徑和字符編碼的文件中讀取文檔內容,以今生成本文檔。 /// </summary> /// <param name="路徑">完整的路徑字符串</param> /// <param name="encoding">編碼格式:默認自動識別。(對於無bom可能識別錯誤)</param> public bool 讀取文檔(string 路徑, Encoding encoding = null) { if (File.Exists(路徑) == false) { return false; } try { if (encoding == null) { encoding = TXT.GetFileEncodeType(路徑); } using (StreamReader SR = new StreamReader(路徑, encoding)) { bool 返回結果 = 讀取文檔(new StringReader(SR.ReadToEnd())); SR.Close(); return 返回結果; } } catch (Exception) { return false; } } /// <summary> /// 從 <see cref="StringReader"/> 中讀取文檔內容,以今生成本文檔。 /// </summary> /// <param name="MyStringReader">StringReader,能夠由string或StreamReader.ReadToEnd()來生成。</param> /// <returns>〈bool〉返回是否讀取成功。</returns> public bool 讀取文檔(StringReader MyStringReader) { /// <summary>正在分析的節</summary> ININode 當前節 = null; /// <summary>正在分析的項</summary> INIItem 當前項 = null; /// <summary>正在分析的節名</summary> string 當前節名 = null; /// <summary>正在分析的項名</summary> string 當前項名 = null; /// <summary>累計讀取的屬性行的計數</summary> int 計數 = 0; /// <summary>該行是合法有效的行,仍是沒法識別的行。(沒法識別做爲備註處理)</summary> bool 有效行 = false; /// <summary>該行去掉空格和Tab符的文本長度</summary> int 有效文本長度; /// <summary>每一個實體前的註釋</summary> string 備註 = ""; // * 循環讀取每行內容 * while (true) { string 行文本 = MyStringReader.ReadLine(); if (行文本 == null) { if (備註.Length > 0) { EndText = 備註; } 上次讀取的有效行數 = 計數; break; } else { string 行; 有效行 = false; // * 獲取 去掉空格和Tab符的文本 * 行 = 行文本.Trim(' ', '\t'); // * 獲取 去掉空格和Tab符的文本的長度 * 有效文本長度 = 行.Length; // * 檢測註釋符 * if (行文本.Contains(U註釋)) { int 註釋位置 = 行文本.IndexOf(U註釋); 行 = 行文本.Substring(0, 註釋位置); int 註釋開始位置 = 註釋位置 + U註釋.Length - 1; int 註釋長度 = 行文本.Length - 註釋開始位置; if (註釋長度 > 0) { if (備註.Length > 0) { 備註 += "\r\n"; } 備註 += 行文本.Substring(註釋開始位置, 註釋長度); } 有效行 = true; } if (行文本.Contains(U註釋2)) { int 註釋位置 = 行文本.IndexOf(U註釋2); 行 = 行文本.Substring(0, 註釋位置); int 註釋開始位置 = 註釋位置 + U註釋2.Length - 1; int 註釋長度 = 行文本.Length - 註釋開始位置; if (註釋長度 > 0) { if (備註.Length > 0) { 備註 += "\r\n"; } 備註 += 行文本.Substring(註釋開始位置, 註釋長度); } 有效行 = true; } // * 檢查開頭字符 * if (行.Length >= 2) { //[類型定義]====首字符:U節首[ if (行[0] == U左括號[0]) { int 右括號位置 = 行.IndexOf(U右括號[0], 2); if (右括號位置 > 1) { 當前節名 = 行.Substring(1, 右括號位置 - 1); 當前節 = New(當前節名, 備註); 備註 = ""; 計數 += 1; 有效行 = true; } } //項定義====含有等號的行 // -> 獲取賦值符號位置 int 賦值符位置 = 行.IndexOf(U等號, 2); if (賦值符位置 > 1) { // -> 得到名稱和值,並新建項 當前項名 = 行.Substring(0, 賦值符位置).Trim(' ', '\t'); string 值 = 行.Substring(賦值符位置 + 1, 行.Length - 賦值符位置 - 1).Trim(' ', '\t'); if (當前節 != null) { 當前項 = 當前節.New(當前項名, 值, 備註); 備註 = ""; 計數 += 1; 有效行 = true; } } } // * 無效行做爲備註處理 * if (有效行 == false) { if (忽略備註 == false) { if (行文本.Length == 0) { 備註 += "\r\n"; } else { 備註 += 行文本 + "\r\n"; } } } } } return true; } #endregion
◇ 編碼問題
1 /// <summary> 2 /// 經過文件的頭部開始的兩個字節來區分一個文件屬於哪一種編碼。 3 /// 若是文件長度不足2字節,則返回null 4 /// 當FF FE時,是Unicode; 5 /// 當FE FF時,是BigEndianUnicode; 6 /// 當EF BB時,是UTF-8; 7 /// 當它不爲這些時,則是ANSI編碼。 8 /// </summary> 9 public static Encoding GetFileEncodeType(string filename) { 10 FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read); 11 BinaryReader br = new BinaryReader(fs); 12 Byte[] buffer = br.ReadBytes(2); 13 if (buffer.Length < 2) { return null; } 14 if (buffer[0] >= 0xEF) { 15 if (buffer[0] == 0xEF && buffer[1] == 0xBB) { 16 return Encoding.UTF8; 17 } else if (buffer[0] == 0xFE && buffer[1] == 0xFF) { 18 return Encoding.BigEndianUnicode; 19 } else if (buffer[0] == 0xFF && buffer[1] == 0xFE) { 20 return Encoding.Unicode; 21 } else { 22 return Encoding.Default; 23 } 24 } else { 25 return Encoding.Default; 26 } 27 }
窗體讀取INI演示
◇ 演示效果
◇ INIListView類
用一個輔助類將INI文件內容顯示到ListView來展示效果。
給每一個節點添加一個Group組,將節點自己和下轄的項都放進組。
當鼠標選中某項時,判斷該item的Key和Group便可知道它屬於哪一個節點,名稱是什麼。
1 public class INIListView { 2 public ListView 視圖; 3 public Color 節顏色 = Color.FromArgb(0, 153, 153); 4 public Color 節底色 = Color.FromArgb(255, 255, 255); 5 public void 綁定控件(ListView ListView) { 6 視圖 = ListView; 7 初始化(); 8 } 9 public void 載入數據(INI ini) { 10 初始化組(ini); 11 初始化數據(ini); 12 } 13 14 private void 初始化() { 15 視圖.View = View.Tile; 16 視圖.ShowGroups = true; 17 初始化列(); 18 } 19 20 private void 初始化列() { 21 視圖.Columns.Clear(); 22 視圖.Columns.Add("A", "名稱", 220); 23 視圖.Columns.Add("B", "值", 300); 24 視圖.Columns.Add("C", "註釋", 440); 25 } 26 private void 初始化組(INI ini) { 27 if (ini == null) { return; } 28 for (int i = 0; i < ini.Nodes.Count; i++) { 29 string nodeName = ini.Nodes[i].Name; 30 int cc = ini.Nodes[i].Items.Count; 31 string nodeTitle = string.Format("{0} ({1})", nodeName, cc); 32 視圖.Groups.Add(nodeName, nodeTitle); 33 } 34 } 35 36 private void 初始化數據(INI ini) { 37 視圖.Items.Clear(); 38 39 if (ini == null) { return; } 40 for (int i = 0; i < ini.Nodes.Count; i++) { 41 string nodeName = ini.Nodes[i].Name; 42 var nodeitem = 視圖.Items.Add(nodeName, "["+nodeName+"]",0); 43 nodeitem.ForeColor = 節顏色; 44 nodeitem.BackColor = 節底色; 45 46 nodeitem.Group = 視圖.Groups[nodeName]; 47 48 49 for (int j = 0; j < ini.Nodes[i].Items.Count; j++) { 50 var iniitem = ini.Nodes[i].Items[j]; 51 string name = iniitem.Name; 52 string value = iniitem.Value; 53 string comment = iniitem.Comment; 54 var item = 視圖.Items.Add(name, name); 55 item.Group = 視圖.Groups[nodeName]; 56 item.SubItems.Add(value); 57 item.SubItems.Add(comment); 58 } 59 } 60 } 61 62 }
窗體上拖一個ListView(數據視圖)和OpenFileDialog(openINIFileDialog)、和Button(按鈕_讀取文件)
1 public partial class 編輯窗體 : Form { 2 INIListView INIListView = new INIListView(); 3 INI 當前文檔; 4 5 6 public 編輯窗體() { 7 InitializeComponent(); 8 } 9 10 private void 編輯窗體_Load(object sender, EventArgs e) { 11 Width = 1280; 12 Height = 720; 13 初始化數據視圖(); 14 openINIFileDialog.InitialDirectory = Environment.CurrentDirectory; 15 } 16 private void 初始化數據視圖() { 17 INIListView.綁定控件(數據視圖); 18 } 19 20 private void 按鈕_讀取文件_Click(object sender, EventArgs e) { 21 var result = openINIFileDialog.ShowDialog(); 22 if (result == DialogResult.OK) { 23 當前文檔 = new INI(); 24 var 讀取結果 = 當前文檔.讀取文檔(openINIFileDialog.FileName); 25 INIListView.載入數據(當前文檔); 26 } 27 28 29 } 30 31 private void 視圖_1_Click(object sender, EventArgs e) { 32 數據視圖.View = View.Details; 33 } 34 35 private void 視圖_2_Click(object sender, EventArgs e) { 36 數據視圖.View = View.Tile; 37 } 38 39 private void 視圖_3_Click(object sender, EventArgs e) { 40 數據視圖.View = View.List; 41 } 42 43 private void 視圖_4_Click(object sender, EventArgs e) { 44 數據視圖.View = View.SmallIcon; 45 } 46 47 private void 視圖_5_Click(object sender, EventArgs e) { 48 數據視圖.View = View.LargeIcon; 49 } 50 }
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
結語:本文實現了INI文件的構造、讀取和寫入。
實際上經過擴展能夠實現更強大的數據格式。