摘要:[C#] 讀取 INI 檔
INI 檔案,格式如下:
[Section1]
key1=value1
key2=value2
[Section2]
key1=value1
key2=value2
圖:
可以使用兩種方法讀取,一是使用 StreamReader 當作資料串流讀取,一種是調用 Win32 API 方式讀取。
版面配置:
程式碼:
Form 類別
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace TestReadINI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 使用 StreamReader 讀取。
/// </summary>
private void btn_UserStreamRead_Click(object sender, EventArgs e)
{
try
{
using (StreamReader oStreamReader = new StreamReader(Path.Combine(Application.StartupPath, "TestFile.ini"),Encoding.Default))
{
int iResult = -1;
listBox1.Items.Clear();
do
{
AddListBox(oStreamReader.ReadLine());
iResult = oStreamReader.Peek();
} while (iResult != -1);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void AddListBox(string StrText)
{
listBox1.Items.Add(StrText);
}
/// <summary>
/// 使用 Win32 API 讀取。
/// </summary>
private void btn_UseWin32API_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
using (TINI oTINI = new TINI(Path.Combine(Application.StartupPath, "TestFile.ini")))
{
string sResult = oTINI.getKeyValue("Test5", "1"); //Test5: Section;1:Key
this.listBox1.Items.Add(sResult);
}
}
} } |
INI 類別
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Runtime.InteropServices;
namespace TestReadINI {
public class TINI : IDisposable {
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
private bool bDisposed = false;
private string _FilePath = string.Empty;
public string FilePath {
get {
if (_FilePath == null)
return string.Empty;
else
return _FilePath;
}
set {
if (_FilePath != value)
_FilePath = value;
}
}
/// <summary>
/// 建構子。
/// </summary>
/// <param name="path">檔案路徑。</param>
public TINI(string path) {
_FilePath = path;
}
/// <summary>
/// 解構子。
/// </summary>
~TINI() {
Dispose(false);
}
/// <summary>
/// 釋放資源(程式設計師呼叫)。
/// </summary>
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this); //要求系統不要呼叫指定物件的完成項。
}
/// <summary>
/// 釋放資源(給系統呼叫的)。
/// </summary>
protected virtual void Dispose(bool IsDisposing) {
if (bDisposed) {
return;
}
if (IsDisposing) {
//補充:
//這裡釋放具有實做 IDisposable 的物件(資源關閉或是 Dispose 等..)
//ex: DataSet DS = new DataSet();
//可在這邊 使用 DS.Dispose();
//或是 DS = null;
//或是釋放 自訂的物件。
//因為我沒有這類的物件,故意留下這段 code ;若繼承這個類別,
//可覆寫這個函式。
}
bDisposed = true;
}
/// <summary>
/// 設定 KeyValue 值。
/// </summary>
/// <param name="IN_Section">Section。</param>
/// <param name="IN_Key">Key。</param>
/// <param name="IN_Value">Value。</param>
public void setKeyValue(string IN_Section, string IN_Key, string IN_Value) {
WritePrivateProfileString(IN_Section, IN_Key, IN_Value, this._FilePath);
}
/// <summary>
/// 取得 Key 相對的 Value 值。
/// </summary>
/// <param name="IN_Section">Section。</param>
/// <param name="IN_Key">Key。</param>
public string getKeyValue(string IN_Section, string IN_Key) {
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(IN_Section, IN_Key, "", temp, 255, this._FilePath);
return temp.ToString();
}
/// <summary>
/// 取得 Key 相對的 Value 值,若沒有則使用預設值(DefaultValue)。
/// </summary>
/// <param name="Section">Section。</param>
/// <param name="Key">Key。</param>
/// <param name="DefaultValue">DefaultValue。</param>
public string getKeyValue(string Section, string Key, string DefaultValue) {
StringBuilder sbResult = null;
try {
sbResult = new StringBuilder(255);
GetPrivateProfileString(Section, Key, "", sbResult, 255, this._FilePath);
return (sbResult.Length > 0) ? sbResult.ToString() : DefaultValue;
}
catch {
return string.Empty;
}
}
}
}執行結果:
使用 StreamReader 讀取。
使用 Win32 API 讀取。
(TestReadINI)
參考資料:
http://blog.mowd.idv.tw/index.php?pl=765&ct1=6
如有錯誤,歡迎指正。
三小俠 小弟獻醜,歡迎指教



