如何在C#中讀寫INI檔案

來源:互聯網
上載者:User
INI檔案就是副檔名為“ini”的檔案。在Windows系統中,INI檔案是很多,最重要的就是“System.ini”、“System32.ini”和“Win.ini”。該檔案主要存放使用者所做的選擇以及系統的各種參數。使用者可以通過修改INI檔案,來改變應用程式和系統的很多配置。但自從Windows 95的退出,在Windows系統中引入了註冊表的概念,INI檔案在Windows系統的地位就開始不斷下滑,這是因為註冊表的獨特優點,使應用程式和系統都把許多參數和初始化資訊放進了註冊表中。但在某些場合,INI檔案還擁有其不可替代的地位。本文就來探討一下C#是如何對INI進行讀寫操作。

INI檔案的結構
INI檔案是一種按照特點方式排列的文字檔。每一個INI檔案構成都非常類似,由若干段落(section)組成,在每個帶括弧的標題下面,是若干個以單個單詞開頭的關鍵詞(keyword)和一個等號,等號右邊的就是關鍵字對應的值(value)。其一般形式如下: [Section1]
KeyWord1 = Valuel
KeyWord2 = Value2
 ……
[Section2]
KeyWord3 = Value3
KeyWord4 = Value4

C#和Win32 API函數

C#並不像C++,擁有屬於自己的類庫。C#使用的類庫是.Net架構為所有.Net程式開發提供的一個共有的類庫——.Net FrameWork SDK。雖然.Net FrameWork SDK內容十分龐大,功能也非常強大,但還不能面面俱到,至少它並沒有提供直接操作INI檔案所需要的相關的類。在本文中,C#操作INI檔案使用的是Windows系統內建Win32的API函數——WritePrivateProfileString()和GetPrivateProfileString()函數。這二個函數都位於“kernel32.dll”檔案中。

我們知道在C#中使用的類庫都是Managed 程式碼(Managed Code)檔案,而Win32的API函數所處的檔案,都是Unmanaged 程式碼(Unmanaged Code)檔案。這就導致了在C#中不可能直接使用這些Unmanaged 程式碼檔案中的函數。好在.Net架構為了保持對下的相容,也為了充分利用以前的資源,提出了互操作,通過互操作可以實現對Win32的API函數的調用。互操作不僅適用於Win32的API函數,還可以用來訪問託管的COM對象。C#中對Win32的API函數的互操作是通過命名空間“System.Runtime.InteropServices”中的“DllImport”特徵類來實現的。它的主要作用是指示此屬性化方法是作為非託管DLL的輸出實現的。下面代碼就是在C#利用命名空間“System.Runtime.InteropServices”中的“DllImport”特徵類申明上面二個Win32的API函數:

C#申明INI檔案的寫操作函數WritePrivateProfileString():

[ DllImport ( "kernel32" ) ]
private static extern long WritePrivateProfileString ( string
section ,
string key , string val , string filePath ) ;

參數說明:section:INI檔案中的段落;key:INI檔案中的關鍵字;val:INI檔案中關鍵字的數值;filePath:INI檔案的完整的路徑和名稱。

C#申明INI檔案的讀操作函數GetPrivateProfileString():

[ DllImport ( "kernel32" ) ]
private static extern int GetPrivateProfileString ( string section ,
string key , string def , StringBuilder retVal ,
int size , string filePath ) ;

參數說明:section:INI檔案中的段落名稱;key:INI檔案中的關鍵字;def:無法讀取時候時候的預設數值;retVal:讀取數值;size:數值的大小;filePath:INI檔案的完整路徑和名稱。

下面是一個讀寫INI檔案的類 public class INIClass
{
public string inipath;
[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);
/// <summary>
/// 構造方法
/// </summary>
/// <param name="INIPath">檔案路徑</param>
public INIClass(string INIPath)
{
inipath = INIPath;
}
/// <summary>
/// 寫入INI檔案
/// </summary>
/// <param name="Section">項目名稱(如 [TypeName] )</param>
/// <param name="Key">鍵</param>
/// <param name="Value">值</param>
public void IniWriteValue(string Section,string Key,string Value)
{
WritePrivateProfileString(Section,Key,Value,this.inipath);
}
/// <summary>
/// 讀出INI檔案
/// </summary>
/// <param name="Section">項目名稱(如 [TypeName] )</param>
/// <param name="Key">鍵</param>
public string IniReadValue(string Section,string Key)
{
StringBuilder temp = new StringBuilder(500);
int i = GetPrivateProfileString(Section,Key,"",temp,500,this.inipath);
return temp.ToString();
}
/// <summary>
/// 驗證檔案是否存在
/// </summary>
/// <returns>布爾值</returns>
public bool ExistINIFile()
{
return File.Exists(inipath);
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.