C#操作INI配置文件示例詳解
本文實例為大家分享了C#操作INI配置文件示例的具體代碼,供大家參考,具體內(nèi)容如下
源文件地址:C#操作INI配置文件示例
創(chuàng)建如圖所示的控件:
源代碼:
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ū)不能為空!", "錯誤", 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)容,希望對大家的學習有所幫助,也希望大家多多支持我們。
上一篇:C#使用HttpPost請求調(diào)用WebService的方法
欄 目:C#教程
本文標題:C#操作INI配置文件示例詳解
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5566.html
您可能感興趣的文章
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實現(xiàn)判斷當前操作用戶管理角色的方法
- 01-10C#中DataGridView常用操作實例小結
- 01-10C#操作ftp類完整實例
- 01-10asp.net中XML如何做增刪改查操作
- 01-10C#路徑,文件,目錄及IO常見操作匯總
- 01-10C#讀寫INI文件的方法
- 01-10C#操作IIS方法集合
- 01-10C#操作數(shù)據(jù)庫中存取圖片文件的方法
- 01-10C#操作 JSON方法匯總


閱讀排行
本欄相關
- 01-10C#通過反射獲取當前工程中所有窗體并
- 01-10關于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻 器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已
隨機閱讀
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10C#中split用法實例總結
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11ajax實現(xiàn)頁面的局部加載