详解C#如何实现读写ini文件
作者:熊思雨 时间:2022-02-04 23:15:27
介绍
INI文件格式由节、键、值组成。
节
[section]
参数
(键=值)
name=value
1.创建一个ini文件
在Debug目录下创建一个ini文件,写入下面内容,注意编码格式用ANSI。
[Information]
Name=周星星
Gender=男
Age=55
Region=香港
2.创建一个winform界面
如下图
3.添加一个ini管理类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Test1
{
public static class IniFunc
{
/// <summary>
/// 获取值
/// </summary>
/// <param name="section">段落名</param>
/// <param name="key">键名</param>
/// <param name="defval">读取异常是的缺省值</param>
/// <param name="retval">键名所对应的的值,没有找到返回空值</param>
/// <param name="size">返回值允许的大小</param>
/// <param name="filepath">ini文件的完整路径</param>
/// <returns></returns>
[DllImport("kernel32.dll")]
private static extern int GetPrivateProfileString(
string section,
string key,
string defval,
StringBuilder retval,
int size,
string filepath);
/// <summary>
/// 写入
/// </summary>
/// <param name="section">需要写入的段落名</param>
/// <param name="key">需要写入的键名</param>
/// <param name="val">写入值</param>
/// <param name="filepath">ini文件的完整路径</param>
/// <returns></returns>
[DllImport("kernel32.dll")]
private static extern int WritePrivateProfileString(
string section,
string key,
string val,
string filepath);
/// <summary>
/// 获取数据
/// </summary>
/// <param name="section">段落名</param>
/// <param name="key">键名</param>
/// <param name="def">没有找到时返回的默认值</param>
/// <param name="filename">ini文件完整路径</param>
/// <returns></returns>
public static string getString(string section, string key, string def, string filename)
{
StringBuilder sb = new StringBuilder(1024);
GetPrivateProfileString(section, key, def, sb, 1024, filename);
return sb.ToString();
}
/// <summary>
/// 写入数据
/// </summary>
/// <param name="section">段落名</param>
/// <param name="key">键名</param>
/// <param name="val">写入值</param>
/// <param name="filename">ini文件完整路径</param>
public static void writeString(string section, string key, string val, string filename)
{
WritePrivateProfileString(section, key, val, filename);
}
}
}
4.添加winform代码
双击winform界面,加入下面代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Test1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string filename = null;
private void Form1_Load(object sender, EventArgs e)
{
filename = Application.StartupPath + "\\Config.ini";
}
/// <summary>
/// 读取
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Read_Click(object sender, EventArgs e)
{
string names = IniFunc.getString("Information", "Name", null, filename);
string gender = IniFunc.getString("Information", "Gender", null, filename);
string age = IniFunc.getString("Information", "Age", null, filename);
string region = IniFunc.getString("Information", "Region", null, filename);
TextBox_Name.Text = names;
TextBox_Gender.Text = gender;
TextBox_Age.Text = age;
TextBox_Region.Text = region;
}
/// <summary>
/// 写入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Write_Click(object sender, EventArgs e)
{
string names = TextBox_Name.Text;
string gender = TextBox_Gender.Text;
string age = TextBox_Age.Text;
string region = TextBox_Region.Text;
IniFunc.writeString("Information", "Name", names, filename);
IniFunc.writeString("Information", "Gender", gender, filename);
IniFunc.writeString("Information", "Age", age, filename);
IniFunc.writeString("Information", "Region", region, filename);
}
/// <summary>
/// 清空
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Clear_Click(object sender, EventArgs e)
{
TextBox_Name.Text = string.Empty;
TextBox_Gender.Text = string.Empty;
TextBox_Age.Text = string.Empty;
TextBox_Region.Text = string.Empty;
}
}
}
来源:https://blog.csdn.net/qq_38693757/article/details/121675847
标签:C#,读写,ini
0
投稿
猜你喜欢
Java BigDecimal案例详解
2021-09-15 12:03:43
代码分析Android消息机制
2023-07-26 09:44:44
java8 forEach结合Lambda表达式遍历 List操作
2021-07-04 07:22:55
Spring Cloud Gateway不同频率限流的解决方案(每分钟,每小时,每天)
2023-01-05 13:49:34
spring boot2结合mybatis增删改查的实现
2023-07-07 01:17:31
Android中加入名片扫描功能实例代码
2023-07-26 12:16:15
一文详解Java拦截器与过滤器的使用
2021-12-17 03:40:41
springboot搭建访客管理系统的实现示例
2023-09-02 13:10:41
Java编程实现基于TCP协议的Socket聊天室示例
2023-02-17 17:23:13
带你了解Java常用类小结
2023-04-15 14:38:26
Android获取apk签名指纹的md5值(防止重新被打包)的实现方法
2023-06-23 04:26:12
Android LuBan与Compressor图片压缩方式
2022-11-29 01:18:41
在Android中使用WebSocket实现消息通信的方法详解
2022-06-10 06:26:18
Java中的Object.getClass()方法解析
2022-05-06 18:54:48
Intellij IDEA导入JAVA项目并启动(图文教程)
2021-11-05 12:30:18
Android Service生命周期详解
2022-03-21 13:13:55
Spring AOP与AspectJ的对比及应用详解
2022-07-07 12:50:47
详解在idea 中使用Mybatis Generator逆向工程生成代码
2023-09-09 17:05:24
Java设计模式七大原则之单一职责原则详解
2022-05-12 20:48:58
java base64编码、解码的三种方式总结
2022-12-29 22:20:51