本文实例为大家分享了C# *** 作INI配置文件示例的具体代码,供大家参考,具体内容如下
源文件地址:C# *** 作INI配置文件示例
创建如图所示的控件:
源代码:
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("姓名或地区不能为空!", "错误", 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 = ""; } }}
运行结果:
总结以上是内存溢出为你收集整理的C#中INI配置文件的图文代码详解全部内容,希望文章能够帮你解决C#中INI配置文件的图文代码详解所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)