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 ; 指明的路径:编译时指定的 ...
随机推荐
- IE9下不显示select
由于IE8和IE9下不兼容,需要在头部加入: <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7&q ...
- 1、第一课 register_chrdev和register_chrdev_region 创建知识
1. register_chrdev注册字符设备后,有0-256个子设备可用,若major==0,则内核动态申请主设备号.static inline int register_chrdev(unsig ...
- Bootstrap相关优质项目学习清单
1:编码规范 by @mdo编写灵活.稳定.高质量的 HTML 和 CSS 代码的规范 http://codeguide.bootcss.com/ 2:快速.可靠.安全的依赖管理工具.Yarn 缓存了 ...
- Activex调试以及m_hWnd为空 解决办法
1. 点击[开始]->[运行] 命令:regedit.2. 定位到HKEY_LOCALMACHINE -> SOFTWARE -> Microsoft -> Internet ...
- Linux 下查看线程信息
1. 使用 pstree -p PID ps aux | grep firefox | grep -v grepcharles 26058 0.0 0.0 4908 1152 ? ...
- 9个完整android开源app项目
一.photoup 介绍: photoup 是一款开源的相册类app,主要功能是将本地图片提交到facebook上去,虽然他的功能和facebook的远程服务相关,但是本身是可以被当作一款 相册应用的 ...
- [JS Compose] 6. Semigroup examples
Let's we want to combine two account accidently have the same name. , friends: ['Franklin'] } , frie ...
- [Vue] Conditionally Render DOM Elements in Vue.js (v-if v-else v-show)
You can use v-if and v-else to display content based on assertions on your data. Similarly, v-show c ...
- 公布一个基于CSDN Code的学习測试仓库
使用CSDN Code代码托管平台有一段时间了,今天新建立了一个公开的仓库https://code.csdn.net/smstong/learngit/tree/master,供大家測试合并请求等协作 ...
- Xcode编译Undefined symbols for architecture xxx 错误总结
可能会遇到这几种错误:Undefined symbols for architecture armv7Undefined symbols for architecture armv7sUndefine ...