C#操作INI文件的示例代碼
更新時(shí)間:2023年12月29日 15:35:10 作者:lljss2020
這篇文章主要為大家詳細(xì)介紹了C#操作INI文件的相關(guān)知識,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以跟隨上班一起學(xué)習(xí)一下
IniFileOp.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Runtime.InteropServices; namespace xxxxx { class IniFileOp { //#region 聲明讀寫INI文件的API函數(shù) [DllImport("kernel32")] private static extern long WritePrivateProfileString(string section, string key, string val, string filepath); //參數(shù)說明:section:INI文件中的段落;key:INI文件中的關(guān)鍵字;val:INI文件中關(guān)鍵字的數(shù)值;filePath:INI文件的完整的路徑和名稱。 [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); //參數(shù)說明:section:INI文件中的段落名稱;key:INI文件中的關(guān)鍵字;def:無法讀取時(shí)候時(shí)候的缺省數(shù)值;retVal:讀取數(shù)值;size:數(shù)值的大小;filePath:INI文件的完整路徑和名稱 //#endregion public string Path; public IniFileOp(string path) { this.Path = path; } /// <summary> /// 寫INI文件 /// </summary> /// <param name="section">段落</param> /// <param name="key">鍵</param> /// <param name="iValue">值</param> public void IniWriteStr(string section, string key, string iValue) { WritePrivateProfileString(section, key, iValue, this.Path); } /// <summary> /// 讀取INI文件 /// </summary> /// <param name="section">段落</param> /// <param name="key">鍵</param> /// <returns>返回的鍵值</returns> public string IniReadStr(string section, string key) { StringBuilder sb = new StringBuilder(256); int i = GetPrivateProfileString(section, key, "", sb, 256, this.Path); return sb.ToString(); } /// <summary> /// 讀取INI文件 /// </summary> /// <param name="Section">段,格式[]</param> /// <param name="Key">鍵</param> /// <returns>返回byte類型的section組或鍵值組</returns> public byte[] IniReadBytes(string section, string key) { StringBuilder sb = new StringBuilder(256); int i = GetPrivateProfileString(section, key, "", sb, 256, this.Path); string s = sb.ToString(); byte[] byteArray = System.Text.Encoding.Default.GetBytes(s); return byteArray; } } }
ConfigPara.ini
[Param] PauseTime=0
使用
int pauseTime = 0; string path1 = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase; path1 += "ConfigPara.ini"; IniFileOp inifile = new IniFileOp(path1); string str1 = inifile.IniReadStr("Param", "PauseTime"); if (str1 != "") { pauseTime = int.Parse(str1); }
到此這篇關(guān)于C#操作INI文件的示例代碼的文章就介紹到這了,更多相關(guān)C#操作INI內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解C#中的泛型以及編程中使用泛型的優(yōu)點(diǎn)
這篇文章主要介紹了詳解C#中的泛型以及編程中使用泛型的優(yōu)點(diǎn),對泛型的支持時(shí)C#語言中的重要特性,需要的朋友可以參考下2016-02-02詳解C#如何將枚舉以下拉數(shù)據(jù)源的形式返回給前端
這篇文章主要為大家詳細(xì)介紹了C#如何實(shí)現(xiàn)將枚舉以下拉數(shù)據(jù)源的形式返回給前端,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-06-06C#?委托與?Lambda?表達(dá)式轉(zhuǎn)換機(jī)制及弱事件模式下的生命周期詳解
本文介紹了C#委托和Lambda表達(dá)式的工作原理,包括委托的內(nèi)部結(jié)構(gòu)、Lambda表達(dá)式的轉(zhuǎn)換機(jī)制以及弱事件模式下的生命周期管理,感興趣的朋友一起看看吧2025-02-02C#檢測移動(dòng)硬盤并獲取移動(dòng)硬盤盤符的方法
這篇文章主要介紹了利用C#檢測移動(dòng)硬盤并獲取移動(dòng)硬盤盤符2017-12-12