C# ini
介绍C#如何对ini文件进行读写操作,C#可以通过调用【kernel32.dll】文件中的 WritePrivateProfileString()和GetPrivateProfileString()函数分别对ini文件进行读和写操作。包括:读取key的值、保存key的值、读取所有section、读取所有key、移除section、移除key等操作。
目录
1. ini文件介绍
2. 读取操作:包括读取key的值、读取所有section、读取所有key等操作。
3. 写入操作: 包括保存key的值、移除section、移除key等操作。
4. 源码下载:展示运行图及源码下载
1. ini文件介绍
ini文件常用于存储各类应用的配置信息,而内部的文件结构主要包括三个概念:section、key和value。
其中section为各独立的区域块,名称可以为英文、中文。

2. GetPrivateProfileString()函数 :读取操作
C#可以通过调用【kernel32.dll】文件中的 GetPrivateProfileString()函数对ini文件进行读取操作。
官方API:https://msdn.microsoft.com/zh-cn/library/ms724353.aspx
函数签名:
|
1
2
|
[DllImport("kernel32")]private static extern int GetPrivateProfileString(string sectionName, string key, string defaultValue, byte[] returnBuffer, int size, string filePath); |
成员:
sectionName {string | null}:要读区的区域名。若传入null值,第4个参数returnBuffer将会获得所有的section name。
key {string | null}:key的名称。若传入null值,第4个参数returnBuffer将会获得所有的指定sectionName下的所有key name。
defaultValue {string}:key没找到时的返回值。
returnBuffer {byte[]}:key所对应的值。
filePath {string}:ini文件路径。
支持的操作:
1) 获取指定key的值。
2.1 获取指定key的值
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/// <summary>/// 根据Key读取Value/// </summary>/// <param name="sectionName">section名称</param>/// <param name="key">key的名称</param>/// <param name="filePath">文件路径</param>public static string GetValue(string sectionName, string key, string filePath){ byte[] buffer = new byte[2048]; int length = GetPrivateProfileString(sectionName, key, "发生错误", buffer,999, filePath); string rs = System.Text.UTF8Encoding.Default.GetString(buffer, 0, length); return rs;} |
2.2 获取ini文件所有的section名称
注意:中文名称的section要进行转码。
|
1
2
3
4
5
6
7
8
9
10
11
12
|
/// <summary>/// 获取ini文件内所有的section名称/// </summary>/// <param name="filePath">文件路径</param>/// <returns>返回一个包含section名称的集合</returns>public static List<string> GetSectionNames(string filePath){ byte[] buffer = new byte[2048]; int length = GetPrivateProfileString(null, "", "", buffer, 999, filePath); String[] rs = System.Text.UTF8Encoding.Default.GetString(buffer, 0, length).Split(new string[] { "\0" },StringSplitOptions.RemoveEmptyEntries); return rs.ToList();} |
2.3 获取指定section下的所有key名称
同样要对中问名称的key进行转码。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/// <summary>/// 获取指定section内的所有key/// </summary>/// <param name="sectionName">section名称</param>/// <param name="filePath">文件路径</param>/// <returns>返回一个包含key名称的集合</returns>public static List<string> GetKeys(string sectionName, string filePath){ byte[] buffer = new byte[2048]; int length = GetPrivateProfileString(sectionName,null,"", buffer, 999, filePath); String[] rs = System.Text.UTF8Encoding.Default.GetString(buffer, 0, length).Split(new string[] { "\0" }, StringSplitOptions.RemoveEmptyEntries); return rs.ToList();} |
3. WritePrivateProfileString()函数:写入操作
C#可以通过调用【kernel32.dll】文件中的 WritePrivateProfileString()函数对ini文件进行写入操作。
官方API:https://msdn.microsoft.com/zh-cn/library/ms725501.aspx
函数签名:
|
1
2
|
[DllImport("kernel32")]private static extern long WritePrivateProfileString(string sectionName, string key, string value, string filePath); |
成员:
sectionName {string}:要写入的区域名。
key {string | null}:key的名称。若传入null值,将移除指定的section。
value {string | null}:设置key所对应的值。若传入null值,将移除指定的key。
filePath {string}:ini文件路径。
支持的操作:
1) 创建/设置key的值。
2) 移除指定的section。
3) 移除指定的key。
3.1 创建/设置key的值
注意:若此key不存在将会创建,否则就为修改此key的值。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/// <summary>/// 保存内容到ini文件/// <para>若存在相同的key,就覆盖,否则就增加</para>/// </summary>/// <param name="sectionName">section名称</param>/// <param name="key">key的名称</param>/// <param name="value">存储的值</param>/// <param name="filePath">文件路径</param>public static bool SetValue(string sectionName, string key, string value, string filePath){ int rs = (int)WritePrivateProfileString(sectionName, key, value, filePath); return rs > 0;} |
3.2 移除指定的section
说明:key参数传入null就为移除指定的section。
|
1
2
3
4
5
6
7
8
9
10
11
|
/// <summary>/// 移除指定的section/// </summary>/// <param name="sectionName">section名称</param>/// <param name="filePath">文件路径</param>/// <returns></returns>public static bool RemoveSection(string sectionName, string filePath){ int rs = (int)WritePrivateProfileString(sectionName, null, "", filePath); return rs > 0;} |
3.3 移除指定的key
说明:value参数传入null就为移除指定的key。
|
1
2
3
4
5
6
7
8
9
10
11
|
/// <summary>/// 移除指定的key/// </summary>/// <param name="sectionName">section名称</param>/// <param name="filePath">文件路径</param>/// <returns></returns>public static bool Removekey(string sectionName, string key, string filePath){ int rs = (int)WritePrivateProfileString(sectionName, key, null, filePath); return rs > 0;} |
4. 源码下载
4.1 运行图

4.2 下载地址
百度网盘:http://pan.baidu.com/s/1dEQ3QuP
CSDN:http://download.csdn.net/detail/polk6/9684148
==================================系列文章==========================================
本篇文章:3.4 C# ini文件操作【源码下载】
1. C#相关文章
1.1 C# 基础(一) 访问修饰符、ref与out、标志枚举等等
1.2 C# 基础(二) 类与接口
1.3 C# DateTime日期格式化
2. C#问题区
2.2 未在本地计算机上注册"OraOLEDB.Oracle"提供程序
2.3 C# 发送邮件 附件名称为空
3. 源码示例
3.1 WinForm 天猫2013双11自动抢红包【源码下载】
3.3 C# 条形码操作【源码下载】
3.4 C# ini文件操作【源码下载】
C# ini的更多相关文章
- C# ini文件操作【源码下载】
介绍C#如何对ini文件进行读写操作,C#可以通过调用[kernel32.dll]文件中的 WritePrivateProfileString()和GetPrivateProfileString()函 ...
- Win.ini和注册表的读取写入
最近在做打包的工作,应用程序的配置信息可以放在注册表文件中,但是在以前的16位操作系统下,配置信息放在Win.ini文件中.下面介绍一下Win.ini文件的读写方法和注册表的编程. 先介绍下Win.i ...
- myeclipse 无法启动 java.lang.IllegalStateException: Unable to acquire application service. Ensure that the org.eclipse.core.runtime bundle is resolved and started (see config.ini).
把myeclipse10 按照目录完整拷贝到了另外一台电脑, 另外的目录 原安装目录 D\:\soft\i\myeclipse10 新安装目录 E\:\soft\myeclipse10 双击启动失败, ...
- .NET Core采用的全新配置系统[6]: 深入了解三种针对文件(JSON、XML与INI)的配置源
物理文件是我们最常用到的原始配置的载体,最佳的配置文件格式主要由三种,它们分别是JSON.XML和INI,对应的配置源类型分别是JsonConfigurationSource.XmlConfigura ...
- 纯C#的ini格式配置文件读写
虽然C#里都是添加app.config 并且访问也很方便 ,有时候还是不习惯用他.那么我们来做个仿C++下的那种ini配置文件读写吧,其他人写的都是调用非托管kernel32.dll.我也用过 但是感 ...
- C#读取ini文件的方法
最近项目用到ini文件,读取ini文件,方法如下: using System; using System.Collections.Generic; using System.Linq; using S ...
- IIS不能下载ini文件
1.打开IIS. 2.选择站点或者存放*.ini文件的目录,右键菜单中选择属性. 3.选择“HTTP头”选项卡. 4.点击“MINE类型”. 5.点击“新建”. 6.这是跳出一个对话框,在“扩展名”一 ...
- 1201MySQL配置文件mysql.ini参数详解
转自http://www.cnblogs.com/feichexia/archive/2012/11/27/mysqlconf.html my.ini(Linux系统下是my.cnf),当mysql服 ...
- PHP7中php.ini、php-fpm和www.conf的配置
引自:https://typecodes.com/web/php7configure.html 1 配置php.ini php.ini是php运行核心配置文件: ######避免PHP信息暴露在htt ...
- php.ini 中文注释
这个文件控制了PHP许多方面的观点.为了让PHP读取这个文件,它必须被命名为 ; ´php.ini´.PHP 将在这些地方依次查找该文件:当前工作目录:环境变量PHPRC ; 指明的路径:编译时指定的 ...
随机推荐
- [array] leetCode-1-Two Sum-Easy
leetCode-1-Two Sum-Easy descrition Given an array of integers, return indices of the two numbers suc ...
- ZOJ 1914 Arctic Network (POJ 2349 UVA 10369) MST
ZOJhttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1914 POJhttp://poj.org/problem?id=23 ...
- angular项目中各个文件的作用
原文地址 https://www.jianshu.com/p/176ea79a7101 大纲 1.对angular项目中的一些文件的概述 2.对其中一些文件的详细描述 2.1.package.json ...
- Spring Cloud底层原理
目录 一.业务场景介绍 二.Spring Cloud核心组件:Eureka 三.Spring Cloud核心组件:Feign 四.Spring Cloud核心组件:Ribbon 五.Spring Cl ...
- 使用Verdi理解RTL design
使用Verdi理解RTL design 接触到一些RTL代码,在阅读与深入理解的过程中的一些思考记录 协议与设计框图 认真反复阅读理解相关协议与设计框图,一个design的设计文档中,设计框图展示了这 ...
- Booth算法
Booth算法 算法描述(载自维基百科) 对于N位乘数Y,布斯算法检查其2的补码形式的最后一位和一个隐含的低位,命名为y-1,初始值为0.对于yi, i = 0, 1, ..., N - 1,考察yi ...
- 终端中经常使用的shell 命令
Mac 在shell命令终端中,Ctrl+n相当于方向向下的方向键,Ctrl+p相当于方向向上的方向键. 在命令终端中通过它们或者方向键能够实现对历史命令的高速查找.这也是高速输入命令的技巧. 在命令 ...
- ios开发级联菜单(利用父子控制器--两个菜单封装为两个子控制器来实现)
一:1:级联菜单可以使用两个tableView来实现,也可以利用父子控制器,两个控制器来实现,根视图控制器作为两个控制器的父控制器,来管理两个子控制器.2:将左右菜单分别交给两个控制器去管理,对于一些 ...
- [Vue] Dynamic Vue.js Components with the component element
You can dynamically switch between components in a template by using the reserved <component> ...
- 一步步学习Linux多任务编程
系统调用 01.什么是系统调用? 02.Linux系统调用之I/O操作(文件操作) 03.文件描述符的复制:dup(), dup2() 多进程实现多任务 04.进程的介绍 05.Linux可执行文件结 ...