標籤:blog 使用 檔案 art cti for
最近用到儲存檔案的相關東西,主要是封裝兩個函數,代碼如下:可以直接使用
using System;using System.Collections.Specialized;using System.IO;using System.Runtime.InteropServices;using System.Text;namespace IniFileTest{ public class INI { const int COUNT = 0xFFFF; [DllImport("kernel32")] private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath); [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string def, byte[] buffer, int size, string filePath); /// <summary> /// ini關聯的檔案 /// </summary> private string FileName; public INI(string filename) { FileInfo file = new FileInfo(filename); if (file.Exists) { FileName = new FileInfo(filename).FullName; } } /// <summary> /// 刪除Section /// </summary> /// <param name="SectionName"></param> /// <returns></returns> public bool EraseSection(string SectionName) { return WritePrivateProfileString(SectionName, null, null, FileName); } /// <summary> /// 寫入section 索引值對,如果沒有section 則建立並寫入 /// </summary> /// <param name="section"></param> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> public bool Write(string section, string key, string value) { return WritePrivateProfileString(section, key, value, FileName); } /// <summary> /// 刪除鍵 /// </summary> /// <param name="section"></param> /// <param name="key"></param> /// <returns></returns> public bool EraseKey(string section, string key) { return WritePrivateProfileString(section, key, null, FileName); } /// <summary> /// 擷取所有section /// </summary> /// <returns></returns> public StringCollection GetSections() { StringCollection sections = new StringCollection(); byte[] buffer = new byte[COUNT]; int length = GetPrivateProfileString(null, null, null, buffer, COUNT, FileName); int start = 0; for (int i = 0; i < length; i++) { if (buffer[i] == 0 && i > start) { string str = Encoding.Default.GetString(buffer, start, i - start); sections.Add(str); start = i + 1; ; } } return sections; } /// <summary> /// 擷取section下的所喲keys /// </summary> /// <param name="section"></param> /// <returns></returns> public StringCollection GetSectionKeys(string section) { StringCollection keys = new StringCollection(); byte[] buffer = new byte[COUNT]; int length = GetPrivateProfileString(section, null, null, buffer, COUNT, FileName); int start = 0; for (int i = 0; i < length; i++) { if (buffer[i] == 0 && i > start) { string str = Encoding.Default.GetString(buffer, start, i - start); keys.Add(str); start = i + 1; ; } } return keys; } /// <summary> /// 查尋指定的key的值,沒有則返回defaultvalue /// </summary> /// <param name="section"></param> /// <param name="key"></param> /// <param name="defaultvalue"></param> /// <returns></returns> public String GetValue(String section, string key, string defaultvalue) { byte[] bytes = new byte[COUNT]; int s = GetPrivateProfileString(section, key, defaultvalue, bytes, COUNT, FileName); string str = Encoding.GetEncoding(0).GetString(bytes, 0, s); return defaultvalue; } }}