欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

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

时间:2023-03-09 12:09:25|栏目:.NET代码|点击:

介绍

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;
        }
    }
}

上一篇:Visual Studio 2022智能辅助编码介绍

栏    目:.NET代码

下一篇:时间戳与时间相互转换(php .net精确到毫秒)

本文标题:详解C#如何实现读写ini文件

本文地址:http://www.codeinn.net/misctech/227123.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有