您好,登錄后才能下訂單哦!
C#中怎么操作INI配置文件,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
源代碼:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace WindowsFormsApplication3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } [DllImport("kernel32.dll")] private static extern long WritePrivateProfileString(string section, string key, string value, string filepath); [DllImport("kernel32.dll")] private static extern int GetPrivateProfileString(string section,string key,string def,StringBuilder returnvalue,intbuffersize,string filepath); private string IniFilePath; private void Form1_Load(object sender, EventArgs e) { comboBox1.Text = "男"; for (int i = 1; i <= 100; i++) { comboBox2.Items.Add(i.ToString()); } comboBox2.Text = "18"; IniFilePath = Application.StartupPath + "\\Config.ini"; } private void button1_Click(object sender, EventArgs e) { if ((textBox1.Text.Trim() != "") && (textBox2.Text.Trim() != "")) { string Section = "Information"; try { WritePrivateProfileString(Section, "Name", textBox1.Text.Trim(), IniFilePath); WritePrivateProfileString(Section, "Gender", comboBox1.Text, IniFilePath); WritePrivateProfileString(Section, "Age", comboBox2.Text, IniFilePath); WritePrivateProfileString(Section, "Region", textBox2.Text.Trim(), IniFilePath); } catch (Exception ee) { MessageBox.Show(ee.Message); } } else { MessageBox.Show("姓名或地區(qū)不能為空!", "錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } private void button2_Click(object sender, EventArgs e) { string outString; try { GetValue("Information", "Name", out outString); textBox1.Text = outString; GetValue("Information", "Gender", out outString); comboBox1.Text = outString; GetValue("Information", "Age", out outString); comboBox2.Text = outString; GetValue("Information", "Region", out outString); textBox2.Text = outString; } catch (Exception ee) { MessageBox.Show(ee.Message); } } private void GetValue(string section,string key, out string value) { StringBuilder stringBuilder = new StringBuilder(); GetPrivateProfileString(section, key, "", stringBuilder, 1024, IniFilePath); value = stringBuilder.ToString(); } private void button3_Click(object sender, EventArgs e) { textBox1.Text = ""; comboBox1.Text = "男"; comboBox2.Text = "18"; textBox2.Text = ""; } } }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。
免責(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)容。