C#操作INI配置文件示例详解
作者:cnc 时间:2021-11-06 11:03:01
本文实例为大家分享了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,配置文件
0
投稿
猜你喜欢
Studio 编译报错:compileSdkVersion 'android-24' requires JDK 1.8 or later to compile.的解决办法
2023-06-19 17:19:41
Android中访问证书有问题的SSL网页的方法
2023-11-20 10:22:00
Java 合并多个MP4视频文件
2022-08-23 12:09:19
Java并发框架:Executor API详解
2021-10-31 14:30:28
Java实现解析JSON大文件JsonReader工具详解
2023-08-21 21:13:14
PageHelper在springboot+mybatis框架中的使用步骤及原理解析
2023-07-28 21:40:09
java控制台输入示例分享
2023-11-28 02:28:05
SpringMVC静态资源访问问题如何解决
2021-06-04 15:58:55
Java中对list map根据map某个key值进行排序的方法
2023-09-04 17:10:03
详解Spring Boot Admin监控服务上下线邮件通知
2023-06-16 21:53:31
在Unity中捕捉Android的常用按钮返回事件
2023-01-31 00:11:36
java为什么会出现精度丢失这种现象你知道吗
2023-07-04 05:24:38
Android使用AutoCompleteTextView实现自动填充功能的案例
2023-03-26 06:56:47
全面了解Java中Native关键字的作用
2022-07-13 02:01:09
Android 字符串中某个字段可点击和设置颜色的方法
2023-07-24 00:16:53
C++语言io流处理基本操作教程示例
2023-11-02 22:07:39
spring获取bean的源码解析
2023-10-11 22:15:43
深入分析NTFS中文件被锁定导致Process.Start失败的详解
2023-08-24 06:11:25
WPF+SkiaSharp实现自绘拖曳小球
2023-03-25 01:31:14
C++继承详细介绍
2022-08-18 10:05:42