一个完整的INI文件格式由节(section)、键(key)、值(value)组成。
示例如:
[section]
key1=value1
key2=value2
; 备注:value的值不要太长,理论上最多不能超过65535个字节。

在Windows程序开发中经常会遇到读写INI配置文件的情况,以下C#类封装了对INI配置文件读写修改的操作

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

namespace CRApp
{
    public class INIHelper
    {
        private string FilePath;
        public string Path
        {
            get { return this.FilePath; }
            set
            {
                if (value.Substring(0, 1) == "\\" || value.Substring(0, 1) == "/")
                {
                    this.FilePath = AppDomain.CurrentDomain + value;
                }
                else
                {
                    this.FilePath = value;
                }
            }
        }
        [DllImport("kernel32")]
        private static extern bool 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);
        public INIHelper(string _Path)
        {
            this.Path = _Path;
        }
        public void WriteValue(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, this.FilePath);
        }
        public string ReadValue(string Section, string Key)
        {
            try
            {
                StringBuilder temp = new StringBuilder();
                int i = GetPrivateProfileString(Section, Key, "", temp, -1, this.FilePath);
                return temp.ToString();
            }
            catch { return ""; }
        }
        public void RemoveKey(string Section, string Key)
        {
            WritePrivateProfileString(Section, Key, null, this.FilePath);
        }
        public bool RemoveSection(string Section)
        {
            return WritePrivateProfileString(Section, null, null, this.FilePath);
        }
        public bool Exists()
        {
            return System.IO.File.Exists(this.FilePath);
        }
    }
}

参考:
[1] WritePrivateProfileString 函数: https://baike.baidu.com/item/WritePrivateProfileString/9711454
[2] GetPrivateProfileString 函数: https://baike.baidu.com/item/GetPrivateProfileString/9642262

C#操作读写INI配置文件的更多相关文章

  1. C# 读写 ini 配置文件

    虽说 XML 文件越发流行,但精简的 ini 配置文件还是经常会用到,在此留个脚印. 当然,文中只是调用系统API,不会报错,如有必要,也可以直接以流形式读取 ini文件并解析. /// <su ...

  2. 引用“kernel32”读写ini配置文件

    引用"kernel32"读写ini配置文件 unity ini kernel32 配置文件  引用"kernel32"读写ini配置文件 OverView ke ...

  3. C# 文件的一些基本操作(转)//用C#读写ini配置文件

    C# 文件的一些基本操作 2009-07-19  来自:博客园  字体大小:[大 中 小] 摘要:介绍C#对文件的一些基本操作,读写等. using System;using System.IO;us ...

  4. [转]VB 读写ini 配置文件

    转自 百度知道 C# 读写 ini配置文件 点此链接 'API 声明Public Declare Function GetPrivateProfileString Lib "kernel32 ...

  5. 自己写的 读写 ini 配置文件类

    /// <summary> /// 不调用系统API 读写 ini 配置文件 /// </summary> public class RW_ini { #region ==== ...

  6. C#+Access 员工信息管理--简单的增删改查操作和.ini配置文件的读写操作。

    1.本程序的使用的语言是C#,数据库是Access2003.主要是对员工信息进行简单的增删改查操作和对.ini配置文件的读写操作. 2.代码运行效果如下: 功能比较简单.其中在得到查询结果后,在查询结 ...

  7. c#读写ini配置文件示例

    虽然c#里都是添加app.config 并且访问也很方便 ,有时候还是不习惯用他.那么我们来做个仿C++下的那种ini配置文件读写吧     其他人写的都是调用非托管kernel32.dll.我也用过 ...

  8. C++读写ini配置文件GetPrivateProfileString()&WritePrivateProfileString()

    转载: 1.https://blog.csdn.net/fengbingchun/article/details/6075716 2. 转自:http://hi.baidu.com/andywangc ...

  9. 使用C#读写ini配置文件

    INI就是扩展名为"INI"的文件,其实他本身是个文本文件,可以用记事本打工,主要存放的是用户所做的选择或系统的各种参数. INI文件其实并不是普通的文本文件.它有自己的结构.由若 ...

随机推荐

  1. 读取 properties 配置文件含有中文的value内容 导致中文乱码 的解决办法

    1.前言 因为装系统的时候把中文写在了系统路径,现在我想把这个路径写在properties里面来读取,可是 发现java 读取会导致中文乱码成 问号????的乱码  ,百度找了好多博客,基本都是一摸一 ...

  2. 介绍一下主流的浏览器的开发者工具(js调试和查看网络请求)

    1.打开开发者工具:右键-->检查 (快捷键 f12) 2.开发者工具介绍: (1): 选择页面的dom进行查看 (2):设备适配 (3)元素: ① 可以查找到界面对应的dom: ② 通过计算样 ...

  3. leetcode 1288. 删除被覆盖区间

    问题描述 给你一个区间列表,请你删除列表中被其他区间所覆盖的区间. 只有当 c <= a 且 b <= d 时,我们才认为区间 [a,b) 被区间 [c,d) 覆盖. 在完成所有删除操作后 ...

  4. 安全检测服务如何帮助社交类App提升应用自身和用户个人安全

    社交类App如今人手必备,且大部分功能.业务活动和产品价值均与用户紧密联系,流量的多少甚至影响着一款应用的生命周期.因此,开发者们开始关注内容合规.治理黑产.防盗防爬等应用安全方面的能力.识别虚假流量 ...

  5. golang中使用kafka客户端sarama消费时需要注意的一个点

    kafka消费者的Consume()方法会阻塞: 当Consume()方法返回err时,不确定继续消费有没有问题:保险起见,退出进程,然后重新初始化. 当Consume()方法返回nil是,是可以继续 ...

  6. 从容器镜像的选择-alpine 说起

    在 容器崛起之前,Alpine 还是个无名之辈,可能是因为大家并不是很关心操作系统本身的大小,毕竟大家只关心业务数据和文档,程序.库文件和系统本身的大小通常可以忽略不计. 容器技术席卷整个软件产业之后 ...

  7. golang中自定义一些类型和对应类型的指针方法

    package main import "fmt" // 项目开发中可以为type声明的类型编写一些方法,从而实现对象.方法的操作 // 声明类型 type myInt int / ...

  8. 《手把手教你》系列技巧篇(六十一)-java+ selenium自动化测试 - 截图三剑客 -下篇(详细教程)

    1.简介 按照计划宏哥今天将介绍java+ selenium自动化测试截图操作实现的第三种截图方法,也就是截图的第三剑客 - 截取某个元素(或者目标区域)的图片.在测试的过程中,有时候不需要截取整个屏 ...

  9. 在树莓派上开发SpringBoot 之使用VSCode远程开发

    一些运行在ARM单板电脑上的IoT应用通常会提供RESTful风格的API接口.本次的文章记录如何在本地电脑上通过VS Code的远程开发功能,在树莓派端创建一个SpringBoot工程,并实现调试和 ...

  10. numpy常用函数记录

    np.square() 函数返回一个新数组,该数组的元素值为源数组元素的平方. 源阵列保持不变. 示例: import numpy as np a = np.array([[1, 2, 3], [4, ...