C#操作INI文件的示例代碼
更新時間:2023年12月29日 15:35:10 作者:lljss2020
這篇文章主要為大家詳細介紹了C#操作INI文件的相關知識,文中的示例代碼講解詳細,具有一定的借鑒價值,感興趣的小伙伴可以跟隨上班一起學習一下
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文件中的關鍵字;val:INI文件中關鍵字的數(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文件中的關鍵字;def:無法讀取時候時候的缺省數(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);
}
到此這篇關于C#操作INI文件的示例代碼的文章就介紹到這了,更多相關C#操作INI內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C#Process的OutputDataReceived事件不觸發(fā)問題及解決
這篇文章主要介紹了C#Process的OutputDataReceived事件不觸發(fā)問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
C#使用Selenium+PhantomJS抓取數(shù)據(jù)
本文主要介紹了C#使用Selenium+PhantomJS抓取數(shù)據(jù)的方法步驟,具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02

