C# Winform 調(diào)用系統(tǒng)接口操作 INI 配置文件的代碼
更新時(shí)間:2011年05月14日 16:00:44 作者:
封裝了一小段代碼, 調(diào)用系統(tǒng)接口, 操作配置文件. 一般用于 .ini 文件, 或者其它鍵值對(duì)格式的配置文件
包括了寫(xiě)入和讀取功能. 寫(xiě)入的時(shí)候, 如果文件不存在會(huì)自動(dòng)創(chuàng)建. 如果對(duì)應(yīng)的鍵已經(jīng)存在, 則自動(dòng)覆蓋它的值. 讀取的時(shí)候, 如果對(duì)應(yīng)的文件不存在, 或者鍵名不存在, 則返回一個(gè) empty 值. 非常方便 ^_^
// 系統(tǒng)接口類(lèi)
public static class WinAPI
{
[DllImport("kernel32")] // 寫(xiě)入配置文件的接口
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);
// 向配置文件寫(xiě)入值
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();
}
}
復(fù)制代碼 代碼如下:
// 系統(tǒng)接口類(lèi)
public static class WinAPI
{
[DllImport("kernel32")] // 寫(xiě)入配置文件的接口
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);
// 向配置文件寫(xiě)入值
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();
}
}
您可能感興趣的文章:
- 詳解C#如何實(shí)現(xiàn)讀寫(xiě)ini文件
- C#實(shí)現(xiàn)ini文件讀寫(xiě)操作
- C#中讀寫(xiě)INI配置文件的方法
- C#操作INI文件的輔助類(lèi)IniHelper
- C#操作INI配置文件示例詳解
- Windows系統(tǒng)中C#讀寫(xiě)ini配置文件的程序代碼示例分享
- C#讀寫(xiě)INI文件的方法
- C#實(shí)現(xiàn)利用Windows API讀寫(xiě)INI文件的方法
- C#實(shí)現(xiàn)讀寫(xiě)ini文件類(lèi)實(shí)例
- c#讀寫(xiě)ini配置文件示例
- C# Ini文件操作實(shí)例
- c#實(shí)現(xiàn)ini文件讀寫(xiě)類(lèi)分享
- C#中讀寫(xiě)INI文件的方法例子
- C#操作ini文件的幫助類(lèi)
相關(guān)文章
WPF實(shí)現(xiàn)自帶觸控鍵盤(pán)的文本框
這篇文章實(shí)現(xiàn)了WPF自帶觸控鍵盤(pán)的文本框,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10C#微信公眾號(hào)與訂閱號(hào)接口開(kāi)發(fā)示例代碼
這篇文章主要介紹了C#微信公眾號(hào)與訂閱號(hào)接口開(kāi)發(fā)示例代碼,結(jié)合實(shí)例形式簡(jiǎn)單分析了C#針對(duì)微信接口的調(diào)用與處理技巧,需要的朋友可以參考下2016-06-06利用Aspose.Word控件實(shí)現(xiàn)Word文檔的操作
偶然一次機(jī)會(huì),一個(gè)項(xiàng)目的報(bào)表功能指定需要導(dǎo)出為Word文檔,因此尋找了很多篇文章,不過(guò)多數(shù)介紹的比較簡(jiǎn)單一點(diǎn),于是也參考了官方的幫助介紹,終于滿足了客戶的需求。下面我由淺入深來(lái)介紹這個(gè)控件在實(shí)際業(yè)務(wù)中的使用過(guò)程吧2013-05-05