详解C#如何实现读写ini文件

目录

  • 介绍
  • 1.创建一个ini文件
  • 2.创建一个winform界面
  • 3.添加一个ini管理类
  • 4.添加winform代码

介绍 INI文件格式由节、键、值组成。

[section]
参数
(键=值)
name=value

1.创建一个ini文件 在Debug目录下创建一个ini文件,写入下面内容,注意编码格式用ANSI。
[Information]
Name=周星星
Gender=男
Age=55
Region=香港

2.创建一个winform界面 如下图
详解C#如何实现读写ini文件
文章图片


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{/// /// 获取值/// /// 段落名/// 键名/// 读取异常是的缺省值/// 键名所对应的的值,没有找到返回空值/// 返回值允许的大小/// ini文件的完整路径/// [DllImport("kernel32.dll")]private static extern int GetPrivateProfileString(string section,string key,string defval,StringBuilder retval,int size,string filepath); /// /// 写入/// /// 需要写入的段落名/// 需要写入的键名/// 写入值/// ini文件的完整路径/// [DllImport("kernel32.dll")]private static extern int WritePrivateProfileString(string section,string key,string val,string filepath); /// /// 获取数据/// /// 段落名/// 键名/// 没有找到时返回的默认值/// ini文件完整路径/// 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(); } /// /// 写入数据/// /// 段落名/// 键名/// 写入值/// ini文件完整路径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"; } /// /// 读取/// /// /// 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; } /// /// 写入/// /// /// 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); } /// /// 清空/// /// /// 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; }}}

【详解C#如何实现读写ini文件】到此这篇关于详解C#如何实现读写ini文件的文章就介绍到这了,更多相关C#读写ini文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读