C#操作INI配置文件示例詳解
更新時(shí)間:2017年07月13日 11:41:41 作者:cnc
這篇文章主要為大家詳細(xì)介紹了C#操作INI配置文件示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了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ū)不能為空!", "錯(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 = "";
}
}
}
運(yùn)行結(jié)果:





以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 詳解C#如何實(shí)現(xiàn)讀寫ini文件
- C#實(shí)現(xiàn)ini文件讀寫操作
- C#中讀寫INI配置文件的方法
- C#操作INI文件的輔助類IniHelper
- Windows系統(tǒng)中C#讀寫ini配置文件的程序代碼示例分享
- C#讀寫INI文件的方法
- C#實(shí)現(xiàn)利用Windows API讀寫INI文件的方法
- C#實(shí)現(xiàn)讀寫ini文件類實(shí)例
- c#讀寫ini配置文件示例
- C# Ini文件操作實(shí)例
- c#實(shí)現(xiàn)ini文件讀寫類分享
- C#中讀寫INI文件的方法例子
- C# Winform 調(diào)用系統(tǒng)接口操作 INI 配置文件的代碼
- C#操作ini文件的幫助類
相關(guān)文章
C#實(shí)現(xiàn)給DataGrid單元行添加雙擊事件的方法
這篇文章主要介紹了C#實(shí)現(xiàn)給DataGrid單元行添加雙擊事件的方法,較為詳細(xì)的分析了C#給DataGrid單元添加雙擊事件的步驟及相關(guān)實(shí)現(xiàn)代碼,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07

