这篇文章主要介绍了C#怎么实现读写ini文件,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。1.创建一个C#项目2.创建一个ini文件在Debug目录下创建一个ini文件,写入
这篇文章主要介绍了C#怎么实现读写ini文件,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
在Debug目录下创建一个ini文件,写入下面内容,注意编码格式用ANSI。
Name=周星星
Gender=男
Age=55
Region=香港
如下图
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); } }}
双击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; } }}
感谢你能够认真阅读完这篇文章,希望小编分享的“C#怎么实现读写ini文件”这篇文章对大家有帮助,同时也希望大家多多支持编程网,关注编程网精选频道,更多相关知识等着你来学习!
--结束END--
本文标题: C#怎么实现读写ini文件
本文链接: https://lsjlt.com/news/321185.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0