1.c#读写Ini文件①增加区字段, ②读取区字段, ③删除区, ④删除区字段

IniHelper.cs是一个用于读写INI配置文件的C#类库,提供了增加区字段、读取区字段、删除区及删除区字段的功能。通过DllImport导入kernel32.dll实现对INI文件的操作,确保文件路径正确并创建所需目录。该类适用于处理应用程序的配置信息存储。

配置IniHelper.cs

有对ini文件的增删改查操作:
①增加区字段,
②读取区字段,
③删除区,
④删除区字段

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

//以后默认放入的文件路径为启动文件所在的目录下

public class IniHelper
{
    /// <summary>
    /// 文件名,如 cameral.ini,文件存在debug下的config文件夹中
    /// </summary>
    public string FileName { get; set; }

    [DllImport("kernel32")]
    private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
    [DllImport("kernel32")]
    private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);


    /// <summary>
    /// 不存在文件夹则创建
    /// </summary>
    /// <param name="path">文件夹绝对路径</param>
    private void CreateDirectoryIfNotExists(string path)
    {
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
    }

    /// <summary>
    /// 写入数据
    /// </summary>
    /// <param name="Section">区</param>
    /// <param name="Key">键</param>
    /// <param name="Value">值</param>
    public void SetValue(string Section, string Key, string Value)
    {
        string path = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory,"config",this.FileName);
        CreateDirectoryIfNotExists(Path.GetDirectoryName(path));
        WritePrivateProfileString(Section, Key, Value, path);
    }

    /// <summary>
    /// 读取数据
    /// </summary>
    /// <param name="Section">区</param>
    /// <param name="Key">键</param>
    /// <returns>读取到的数据</returns>
    public string GetValue(string Section, string Key)
    {
        string path = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory,"config",this.FileName);
        CreateDirectoryIfNotExists(Path.GetDirectoryName(path));
        StringBuilder temp = new StringBuilder(500);    //  缓存空间--长度500
        //下面的500指的是读取500个长度放到缓存空间temp中,换句话说temp的长度应当>=下面的500
        int i = GetPrivateProfileString(Section, Key, "", temp, 500, path);    
        return temp.ToString();
    }

    /// <summary>
    /// 删除区
    /// </summary>
    /// <param name="Section"></param>
    public void DeleteSection(string Section)
    {
        string path = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "config", this.FileName);
        CreateDirectoryIfNotExists(Path.GetDirectoryName(path));
        WritePrivateProfileString(Section, null, null, path);
    }

    /// <summary>
    /// 删除字段
    /// </summary>
    /// <param name="Section"></param>
    /// <param name="Key"></param>
    public void DeleteKey(string Section, string Key)
    {
        string path = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "config", this.FileName);
        CreateDirectoryIfNotExists(Path.GetDirectoryName(path));
        WritePrivateProfileString(Section, Key, null, path);
    }


}

下载

链接: 下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱搞事的程小猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值