一个完整的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. 实验 2 :Mininet 实验 —— 拓扑的命令脚本

    实验2: Mininet 实验--拓扑的命令脚本 一.实验目的 掌握 Mininet 的自定义拓扑生成方法:命令行创建.Python 脚本编写 二 .实验任务 通过使用命令行创建.Python 脚本编 ...

  2. 【Java】toString

    toString 当我们输出一个对象的引用时,实际上就是调用当前对象的toString() Object类中toString()的定义: public String toString() { retu ...

  3. 《剑指offer》面试题33. 二叉搜索树的后序遍历序列

    问题描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历结果.如果是则返回 true,否则返回 false.假设输入的数组的任意两个数字都互不相同.   参考以下这颗二叉搜索树: 5 / \ ...

  4. blender建模常用建模快捷键

    编辑物体 M2选取 M2+SHIFT选取多个 A全选 B+M1矩阵选择 C+M1笔刷选择 CTRL+M1套索选择 CTRL+SHIFT+M1取消套索选择 ALT+M2选择边循环,面 CTRL+ALT+ ...

  5. vue学习12-表单属性绑定

    1 <!DOCTYPE html> 2 <html lang='en'> 3 <head> 4 <meta charset='UTF-8'> 5 < ...

  6. C++普通成员函数的调用形式

    #include<iostream> using namespace std; class A { public: int data; void foo(int x) { data = x ...

  7. 关于笨蛋式病毒创作(CMD式)C++

    ​ 对于病毒创作,有好多种原因:有想装逼的,想盗取信息的...... 任何理由千奇百出,有的在下面评论出来. 对于病毒创作,我可是既会有不会,只会乱写,回头一看,全错了. 我今天写的这篇博客就是面向于 ...

  8. QA(测试) 工作准则建议

    身为一个专业的 QA 当然需要有自己的测试原则,这些测试原则不仅可以帮助我们提高产品质量,对外还能体现出我们的专业性,从而让合作方后续还有意愿和我们合作. 1 测试前 1.1 需求评审 必须参与,有问 ...

  9. Lesson3——NumPy 数据类型

    NumPy 教程目录 NumPy 数据类型 numpy 支持的数据类型比 Python 内置的类型要多很多,基本上可以和 C 语言的数据类型对应上,其中部分类型对应为 Python 内置的类型. 下表 ...

  10. 通过json动态创建控制器

    通过字符串来创建控制器 如果通过字符串来创建控制器 不可以直接通过类型来获取对应的类 因为Swift有命名空间,类前需要加上命名空间的名称 获取命名空间的名称 let executable = NSB ...