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)文章
支持多類型數(shù)據(jù)庫的c#數(shù)據(jù)庫模型示例
本文為大家提供一個(gè)c#數(shù)據(jù)庫訪問模型,支持多類型數(shù)據(jù)庫,簡單抽取數(shù)據(jù)庫訪問函數(shù),大家參考使用吧2014-01-01
C#中的應(yīng)用程序接口介紹及實(shí)現(xiàn),密封類與密封方法
今天小編就為大家分享一篇關(guān)于C#中的應(yīng)用程序接口介紹及實(shí)現(xiàn),密封類與密封方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
2018-10-10
C#中使用HttpPost調(diào)用WebService的方法
這篇文章介紹了C#中使用HttpPost調(diào)用WebService的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
2022-03-03
C#使用Socket實(shí)現(xiàn)通信的方法示例
這篇文章主要介紹了C#使用Socket實(shí)現(xiàn)通信的方法示例,文章按照 Socket 的 創(chuàng)建、連接、傳輸數(shù)據(jù)、釋放資源的過程來寫,給出方法、參數(shù)的詳細(xì)信息,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下
2024-06-06 
