时间:2021-05-20
本文实例为大家分享了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 = ""; } }}运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了C#实现读写ini文件类。分享给大家供大家参考。具体如下:这个C#类封装了对INI配置文件进行操作所需的各种函数,包括读取键值、读取键值、删除段落
通常C#使用基于XML的配置文件,不过如果有需要的话,比如要兼顾较老的系统,可能还是要用到INI文件。但C#本身并不具备读写INI文件的API,只有通过调用非托
使用INI配置文件,简单便捷。该辅助工具类为C#操作INI文件的辅助类,源码在某位师傅的基础上完善的来,因为忘记最初的来源了,因此不能提及引用,在此深感遗憾,并
编写C#程序的时候,我们都遇到过配置文件,而如今绝大多数的配置文件都是用XML写的。所以在处理的时候就需要操作XML文件。那么C#如何操作XML文件那?下面跟我
C读取ini文件前言:在Windows下可以用GetPrivateProfileString或GetPrivateProfileInt方便读取.ini配置文件内