您好,登錄后才能下訂單哦!
這篇文章主要介紹了C#怎么實(shí)現(xiàn)讀寫ini文件,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
在Debug目錄下創(chuàng)建一個(gè)ini文件,寫入下面內(nèi)容,注意編碼格式用ANSI。
[Information]
Name=周星星
Gender=男
Age=55
Region=香港
如下圖
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace Test1 { public static class IniFunc { /// <summary> /// 獲取值 /// </summary> /// <param name="section">段落名</param> /// <param name="key">鍵名</param> /// <param name="defval">讀取異常是的缺省值</param> /// <param name="retval">鍵名所對(duì)應(yīng)的的值,沒有找到返回空值</param> /// <param name="size">返回值允許的大小</param> /// <param name="filepath">ini文件的完整路徑</param> /// <returns></returns> [DllImport("kernel32.dll")] private static extern int GetPrivateProfileString( string section, string key, string defval, StringBuilder retval, int size, string filepath); /// <summary> /// 寫入 /// </summary> /// <param name="section">需要寫入的段落名</param> /// <param name="key">需要寫入的鍵名</param> /// <param name="val">寫入值</param> /// <param name="filepath">ini文件的完整路徑</param> /// <returns></returns> [DllImport("kernel32.dll")] private static extern int WritePrivateProfileString( string section, string key, string val, string filepath); /// <summary> /// 獲取數(shù)據(jù) /// </summary> /// <param name="section">段落名</param> /// <param name="key">鍵名</param> /// <param name="def">沒有找到時(shí)返回的默認(rèn)值</param> /// <param name="filename">ini文件完整路徑</param> /// <returns></returns> public static string getString(string section, string key, string def, string filename) { StringBuilder sb = new StringBuilder(1024); GetPrivateProfileString(section, key, def, sb, 1024, filename); return sb.ToString(); } /// <summary> /// 寫入數(shù)據(jù) /// </summary> /// <param name="section">段落名</param> /// <param name="key">鍵名</param> /// <param name="val">寫入值</param> /// <param name="filename">ini文件完整路徑</param> public static void writeString(string section, string key, string val, string filename) { WritePrivateProfileString(section, key, val, filename); } } }
雙擊winform界面,加入下面代碼
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Test1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private string filename = null; private void Form1_Load(object sender, EventArgs e) { filename = Application.StartupPath + "\\Config.ini"; } /// <summary> /// 讀取 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button_Read_Click(object sender, EventArgs e) { string names = IniFunc.getString("Information", "Name", null, filename); string gender = IniFunc.getString("Information", "Gender", null, filename); string age = IniFunc.getString("Information", "Age", null, filename); string region = IniFunc.getString("Information", "Region", null, filename); TextBox_Name.Text = names; TextBox_Gender.Text = gender; TextBox_Age.Text = age; TextBox_Region.Text = region; } /// <summary> /// 寫入 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button_Write_Click(object sender, EventArgs e) { string names = TextBox_Name.Text; string gender = TextBox_Gender.Text; string age = TextBox_Age.Text; string region = TextBox_Region.Text; IniFunc.writeString("Information", "Name", names, filename); IniFunc.writeString("Information", "Gender", gender, filename); IniFunc.writeString("Information", "Age", age, filename); IniFunc.writeString("Information", "Region", region, filename); } /// <summary> /// 清空 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button_Clear_Click(object sender, EventArgs e) { TextBox_Name.Text = string.Empty; TextBox_Gender.Text = string.Empty; TextBox_Age.Text = string.Empty; TextBox_Region.Text = string.Empty; } } }
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“C#怎么實(shí)現(xiàn)讀寫ini文件”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。