C# Winform 調(diào)用系統(tǒng)接口操作 INI 配置文件的代碼
更新時間:2011年05月14日 16:00:44 作者:
封裝了一小段代碼, 調(diào)用系統(tǒng)接口, 操作配置文件. 一般用于 .ini 文件, 或者其它鍵值對格式的配置文件
包括了寫入和讀取功能. 寫入的時候, 如果文件不存在會自動創(chuàng)建. 如果對應的鍵已經(jīng)存在, 則自動覆蓋它的值. 讀取的時候, 如果對應的文件不存在, 或者鍵名不存在, 則返回一個 empty 值. 非常方便 ^_^
// 系統(tǒng)接口類
public static class WinAPI
{
[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);
// 向配置文件寫入值
public static void ProfileWriteValue(
string section, string key, string value, string path)
{
WritePrivateProfileString(section, key, value, path);
}
// 讀取配置文件的值
public static string ProfileReadValue(
string section, string key, string path)
{
StringBuilder sb = new StringBuilder(255);
GetPrivateProfileString(section, key, "", sb, 255, path);
return sb.ToString().Trim();
}
}
復制代碼 代碼如下:
// 系統(tǒng)接口類
public static class WinAPI
{
[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);
// 向配置文件寫入值
public static void ProfileWriteValue(
string section, string key, string value, string path)
{
WritePrivateProfileString(section, key, value, path);
}
// 讀取配置文件的值
public static string ProfileReadValue(
string section, string key, string path)
{
StringBuilder sb = new StringBuilder(255);
GetPrivateProfileString(section, key, "", sb, 255, path);
return sb.ToString().Trim();
}
}
相關文章
利用Aspose.Word控件實現(xiàn)Word文檔的操作
偶然一次機會,一個項目的報表功能指定需要導出為Word文檔,因此尋找了很多篇文章,不過多數(shù)介紹的比較簡單一點,于是也參考了官方的幫助介紹,終于滿足了客戶的需求。下面我由淺入深來介紹這個控件在實際業(yè)務中的使用過程吧2013-05-05