国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#讀寫INI文件的方法

2019-10-29 21:38:00
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了C#讀寫INI文件的方法,涉及C#讀寫ini文件的相關實現技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了C#讀寫INI文件的方法。分享給大家供大家參考。具體如下:

雖然微軟早已經建議在WINDOWS中用注冊表代替INI文件,但是在實際應用中,INI文件仍然有用武之地,尤其現在綠色軟件的流行,越來越多的程序將自己的一些配置信息保存到了INI文件中。

INI文件是文本文件,由若干節(section)組成,在每個帶括號的標題下面,是若干個關鍵詞(key)及其對應的值(Value)

[Section]

Key=Value

VC中提供了API函數進行INI文件的讀寫操作,但是微軟推出的C#編程語言中卻沒有相應的方法,下面是一個C# ini文件讀寫類,從網上收集的,很全,就是沒有對section的改名功能,高手可以增加一個。

 

 
  1. using System; 
  2. using System.IO; 
  3. using System.Runtime.InteropServices; 
  4. using System.Text; 
  5. using System.Collections; 
  6. using System.Collections.Specialized; 
  7. namespace wuyisky 
  8. /// <summary> 
  9. /// IniFiles的類 
  10. /// </summary> 
  11. public class IniFiles 
  12. public string FileName; //INI文件名 
  13. //聲明讀寫INI文件的API函數 
  14. [DllImport("kernel32")] 
  15. private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath); 
  16. [DllImport("kernel32")] 
  17. private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath); 
  18. //類的構造函數,傳遞INI文件名 
  19. public IniFiles(string AFileName) 
  20. // 判斷文件是否存在 
  21. FileInfo fileInfo = new FileInfo(AFileName); 
  22. //Todo:搞清枚舉的用法 
  23. if ((!fileInfo.Exists)) 
  24. //|| (FileAttributes.Directory in fileInfo.Attributes)) 
  25. //文件不存在,建立文件 
  26. System.IO.StreamWriter sw = new System.IO.StreamWriter(AFileName, false, System.Text.Encoding.Default); 
  27. try 
  28. sw.Write("#表格配置檔案"); 
  29. sw.Close(); 
  30. catch 
  31. throw (new ApplicationException("Ini文件不存在")); 
  32. //必須是完全路徑,不能是相對路徑 
  33. FileName = fileInfo.FullName; 
  34. //寫INI文件 
  35. public void WriteString(string Section, string Ident, string Value) 
  36. if (!WritePrivateProfileString(Section, Ident, Value, FileName)) 
  37. throw (new ApplicationException("寫Ini文件出錯")); 
  38. //讀取INI文件指定 
  39. public string ReadString(string Section, string Ident, string Default) 
  40. Byte[] Buffer = new Byte[65535]; 
  41. int bufLen = GetPrivateProfileString(Section, Ident, Default, Buffer, Buffer.GetUpperBound(0), FileName); 
  42. //必須設定0(系統默認的代碼頁)的編碼方式,否則無法支持中文 
  43. string s = Encoding.GetEncoding(0).GetString(Buffer); 
  44. s = s.Substring(0, bufLen); 
  45. return s.Trim(); 
  46. //讀整數 
  47. public int ReadInteger(string Section, string Ident, int Default) 
  48. string intStr = ReadString(Section, Ident, Convert.ToString(Default)); 
  49. try 
  50. return Convert.ToInt32(intStr); 
  51. catch (Exception ex) 
  52. Console.WriteLine(ex.Message); 
  53. return Default; 
  54. //寫整數 
  55. public void WriteInteger(string Section, string Ident, int Value) 
  56. WriteString(Section, Ident, Value.ToString()); 
  57. //讀布爾 
  58. public bool ReadBool(string Section, string Ident, bool Default) 
  59. try 
  60. return Convert.ToBoolean(ReadString(Section, Ident, Convert.ToString(Default))); 
  61. catch (Exception ex) 
  62. Console.WriteLine(ex.Message); 
  63. return Default; 
  64. //寫Bool 
  65. public void WriteBool(string Section, string Ident, bool Value) 
  66. WriteString(Section, Ident, Convert.ToString(Value)); 
  67. //從Ini文件中,將指定的Section名稱中的所有Ident添加到列表中 
  68. public void ReadSection(string Section, StringCollection Idents) 
  69. Byte[] Buffer = new Byte[16384]; 
  70. //Idents.Clear(); 
  71. int bufLen = GetPrivateProfileString(Section, nullnull, Buffer, Buffer.GetUpperBound(0), 
  72. FileName); 
  73. //對Section進行解析 
  74. GetStringsFromBuffer(Buffer, bufLen, Idents); 
  75. private void GetStringsFromBuffer(Byte[] Buffer, int bufLen, StringCollection Strings) 
  76. Strings.Clear(); 
  77. if (bufLen != 0) 
  78. int start = 0; 
  79. for (int i = 0; i < bufLen; i++) 
  80. if ((Buffer[i] == 0) && ((i - start) > 0)) 
  81. String s = Encoding.GetEncoding(0).GetString(Buffer, start, i - start); 
  82. Strings.Add(s); 
  83. start = i + 1; 
  84. //從Ini文件中,讀取所有的Sections的名稱 
  85. public void ReadSections(StringCollection SectionList) 
  86. //Note:必須得用Bytes來實現,StringBuilder只能取到第一個Section 
  87. byte[] Buffer = new byte[65535]; 
  88. int bufLen = 0; 
  89. bufLen = GetPrivateProfileString(nullnullnull, Buffer, 
  90. Buffer.GetUpperBound(0), FileName); 
  91. GetStringsFromBuffer(Buffer, bufLen, SectionList); 
  92. //讀取指定的Section的所有Value到列表中 
  93. public void ReadSectionValues(string Section, NameValueCollection Values) 
  94. StringCollection KeyList = new StringCollection(); 
  95. ReadSection(Section, KeyList); 
  96. Values.Clear(); 
  97. foreach (string key in KeyList) 
  98. Values.Add(key, ReadString(Section, key, "")); 
  99. ////讀取指定的Section的所有Value到列表中, 
  100. //public void ReadSectionValues(string Section, NameValueCollection Values,char splitString) 
  101. //{  string sectionValue; 
  102. //  string[] sectionValueSplit; 
  103. //  StringCollection KeyList = new StringCollection(); 
  104. //  ReadSection(Section, KeyList); 
  105. //  Values.Clear(); 
  106. //  foreach (string key in KeyList) 
  107. //  { 
  108. //    sectionValue=ReadString(Section, key, ""); 
  109. //    sectionValueSplit=sectionValue.Split(splitString); 
  110. //    Values.Add(key, sectionValueSplit[0].ToString(),sectionValueSplit[1].ToString()); 
  111. //  } 
  112. //} 
  113. //清除某個Section 
  114. public void EraseSection(string Section) 
  115. if (!WritePrivateProfileString(Section, nullnull, FileName)) 
  116. throw (new ApplicationException("無法清除Ini文件中的Section")); 
  117. //刪除某個Section下的鍵 
  118. public void DeleteKey(string Section, string Ident) 
  119. WritePrivateProfileString(Section, Ident, null, FileName); 
  120. //Note:對于Win9X,來說需要實現UpdateFile方法將緩沖中的數據寫入文件 
  121. //在Win NT, 2000和XP上,都是直接寫文件,沒有緩沖,所以,無須實現UpdateFile 
  122. //執行完對Ini文件的修改之后,應該調用本方法更新緩沖區。 
  123. public void UpdateFile() 
  124. WritePrivateProfileString(nullnullnull, FileName); 
  125. //檢查某個Section下的某個鍵值是否存在 
  126. public bool ValueExists(string Section, string Ident) 
  127. StringCollection Idents = new StringCollection(); 
  128. ReadSection(Section, Idents); 
  129. return Idents.IndexOf(Ident) > -1; 
  130. //確保資源的釋放 
  131. ~IniFiles() 
  132. UpdateFile(); 

目前C# 對ini文件操作基本上要被xml文件取代了,但是我覺得ini文件的讀寫仍然是編程的基本,是必須會的

希望本文所述對大家的C#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 马龙县| 三穗县| 榆树市| 普洱| 周宁县| 资溪县| 灵丘县| 怀集县| 巴林左旗| 石门县| 通辽市| 双鸭山市| 象州县| 本溪市| 阳泉市| 正宁县| 长顺县| 乐安县| 石河子市| 迭部县| 禄丰县| 岢岚县| 灌云县| 金溪县| 靖西县| 冀州市| 东阳市| 贵溪市| 神农架林区| 铜陵市| 龙江县| 巴林左旗| 寿宁县| 文化| 台东市| 嘉祥县| 广河县| 天祝| 孙吴县| 昆山市| 南靖县|