C#学习笔记——读写ini文件
生活随笔
收集整理的這篇文章主要介紹了
C#学习笔记——读写ini文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1: using System; 2: using System.Collections.Generic; 3: using System.Linq; 4: using System.Text; 5: //引入命名空間 6: using System.Collections.Specialized; 7: using System.IO; 8:? 9:? 10: namespace ConfigFiles 11: { 12: /// <summary> 13: /// IniFiles的類 14: /// </summary> 15: public class IniFiles 16: { 17: public string FileName; //INI文件名 18:? 19: //聲明讀寫INI文件的API函數 20: [System.Runtime.InteropServices.DllImport("kernel32")] 21: private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath); 22: [System.Runtime.InteropServices.DllImport("kernel32")] 23: private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath); 24:? 25: //類的構造函數,傳遞INI文件名 26: public IniFiles(string AFileName) 27: { 28: // 判斷文件是否存在 29: FileInfo fileInfo = new FileInfo(AFileName); 30:? 31: //Todo:搞清枚舉的用法 32: if ((!fileInfo.Exists)) 33: { //|| (FileAttributes.Directory in fileInfo.Attributes)) 34: //文件不存在,建立文件 35: System.IO.StreamWriter sw = new System.IO.StreamWriter(AFileName, false, System.Text.Encoding.Default); 36: try 37: { 38: sw.Write("#應用程序配置文件"); 39: sw.Close(); 40: } 41:? 42: catch 43: { 44: throw (new ApplicationException("Ini文件不存在")); 45: } 46: } 47:? 48: //必須是完全路徑,不能是相對路徑 49: FileName = fileInfo.FullName; 50: } 51:? 52: //寫INI文件 53: public void WriteString(string Section, string Ident, string Value) 54: { 55: if (!WritePrivateProfileString(Section, Ident, Value, FileName)) 56: { 57: throw (new ApplicationException("寫Ini文件出錯")); 58: } 59: } 60:? 61: //讀取INI文件指定 62: public string ReadString(string Section, string Ident, string Default) 63: { 64: Byte[] Buffer = new Byte[65535]; 65: int bufLen = GetPrivateProfileString(Section, Ident, Default, Buffer, Buffer.GetUpperBound(0), FileName); 66: //必須設定0(系統默認的代碼頁)的編碼方式,否則無法支持中文 67: string s = Encoding.GetEncoding(0).GetString(Buffer); 68: s = s.Substring(0, bufLen); 69: return s.Trim(); 70: } 71:? 72: //讀整數 73: public int ReadInteger(string Section, string Ident, int Default) 74: { 75: string intStr = ReadString(Section, Ident, Convert.ToString(Default)); 76: try 77: { 78: return Convert.ToInt32(intStr); 79:? 80: } 81: catch (Exception ex) 82: { 83: Console.WriteLine(ex.Message); 84: return Default; 85: } 86: } 87:? 88: //寫整數 89: public void WriteInteger(string Section, string Ident, int Value) 90: { 91: WriteString(Section, Ident, Value.ToString()); 92: } 93:? 94: //讀布爾 95: public bool ReadBool(string Section, string Ident, bool Default) 96: { 97: try 98: { 99: return Convert.ToBoolean(ReadString(Section, Ident, Convert.ToString(Default))); 100: } 101: catch (Exception ex) 102: { 103: Console.WriteLine(ex.Message); 104: return Default; 105: } 106: } 107:? 108: //寫Bool 109: public void WriteBool(string Section, string Ident, bool Value) 110: { 111: WriteString(Section, Ident, Convert.ToString(Value)); 112: } 113:? 114: //從Ini文件中,將指定的Section名稱中的所有Ident添加到列表中 115: public void ReadSection(string Section, StringCollection Idents) 116: { 117: Byte[] Buffer = new Byte[16384]; 118: //Idents.Clear(); 119:? 120: int bufLen = GetPrivateProfileString(Section, null, null, Buffer, Buffer.GetUpperBound(0), 121: FileName); 122: //對Section進行解析 123: GetStringsFromBuffer(Buffer, bufLen, Idents); 124: } 125:? 126: private void GetStringsFromBuffer(Byte[] Buffer, int bufLen, StringCollection Strings) 127: { 128: Strings.Clear(); 129: if (bufLen != 0) 130: { 131: int start = 0; 132: for (int i = 0; i < bufLen; i++) 133: { 134: if ((Buffer[i] == 0) && ((i - start) > 0)) 135: { 136: String s = Encoding.GetEncoding(0).GetString(Buffer, start, i - start); 137: Strings.Add(s); 138: start = i + 1; 139: } 140: } 141: } 142: } 143:? 144: //從Ini文件中,讀取所有的Sections的名稱 145: public void ReadSections(StringCollection SectionList) 146: { 147: //Note:必須得用Bytes來實現,StringBuilder只能取到第一個Section 148: byte[] Buffer = new byte[65535]; 149: int bufLen = 0; 150: bufLen = GetPrivateProfileString(null, null, null, Buffer, 151: Buffer.GetUpperBound(0), FileName); 152: GetStringsFromBuffer(Buffer, bufLen, SectionList); 153: } 154:? 155: //讀取指定的Section的所有Value到列表中 156: public void ReadSectionValues(string Section, NameValueCollection Values) 157: { 158: StringCollection KeyList = new StringCollection(); 159: ReadSection(Section, KeyList); 160: Values.Clear(); 161: foreach (string key in KeyList) 162: { 163: Values.Add(key, ReadString(Section, key, "")); 164:? 165: } 166: } 167:? 168:? 169: //清除某個Section 170: public void EraseSection(string Section) 171: { 172: // 173: if (!WritePrivateProfileString(Section, null, null, FileName)) 174: { 175:? 176: throw (new ApplicationException("無法清除Ini文件中的Section")); 177: } 178: } 179:? 180: //刪除某個Section下的鍵 181: public void DeleteKey(string Section, string Ident) 182: { 183: WritePrivateProfileString(Section, Ident, null, FileName); 184: } 185:? 186: //Note:對于Win9X,來說需要實現UpdateFile方法將緩沖中的數據寫入文件 187: //在Win NT, 2000和XP上,都是直接寫文件,沒有緩沖,所以,無須實現UpdateFile 188: //執行完對Ini文件的修改之后,應該調用本方法更新緩沖區。 189: public void UpdateFile() 190: { 191: WritePrivateProfileString(null, null, null, FileName); 192: } 193:? 194: //檢查某個Section下的某個鍵值是否存在 195: public bool ValueExists(string Section, string Ident) 196: { 197: // 198: StringCollection Idents = new StringCollection(); 199: ReadSection(Section, Idents); 200: return Idents.IndexOf(Ident) > -1; 201: } 202:? 203: //確保資源的釋放 204: ~IniFiles() 205: { 206: UpdateFile(); 207: } 208: } 209: } 作者:韓兆新 出處:http://hanzhaoxin.cnblogs.com/ 本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。 分類:?[01]C#語言基礎 標簽:?C#學習筆記
本文轉自韓兆新博客博客園博客,原文鏈接:http://www.cnblogs.com/hanzhaoxin/archive/2012/12/31/2840358.html,如需轉載請自行聯系原作者
本文轉自韓兆新博客博客園博客,原文鏈接:http://www.cnblogs.com/hanzhaoxin/archive/2012/12/31/2840358.html,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的C#学习笔记——读写ini文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [SOJ] DAG?
- 下一篇: c# char unsigned_dll