介绍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文件常用于存储各类应用的配置信息,而内部的文件结构主要包括三个概念:sectionkeyvalue

其中section为各独立的区域块,名称可以为英文、中文。

2. GetPrivateProfileString()函数 :读取操作

C#可以通过调用【kernel32.dll】文件中的 GetPrivateProfileString()函数对ini文件进行读取操作。

官方APIhttps://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) 获取ini文件所有的section名称

3) 获取指定section下的所有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文件进行写入操作。

官方APIhttps://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

CSDNhttp://download.csdn.net/detail/polk6/9684148

==================================系列文章==========================================

本篇文章:3.4 C# ini文件操作【源码下载】

C#文章导航

 
分类: C#

最新文章

  1. ClickOnce部署(5):自定义安全权限
  2. SQL中的with as
  3. 基于DCMTK的DICOM相关程序编写攻略
  4. 。。。Spring框架总结(一)。。。
  5. 如何在Unity中播放影片
  6. This configuration file was broken by system-config-keyboard
  7. java基础(十二)常用类总结(二)
  8. Android开源项目(一)
  9. java war 打包、解压命令
  10. hadoop如何查看文件系统
  11. IOS开发中深拷贝与浅拷贝
  12. CUDA编程(六)进一步并行
  13. grails项目中(DB的相关操作)
  14. java 零基础搭建dubbo运行环境
  15. linux环境中,top命令中,对command的命令进行扩展查看详情?
  16. vue-router,vuex
  17. OSGI企业应用开发(四)使用Blueprint整合Spring框架(一)
  18. java===java基础学习(12)---方法的重写和重载
  19. 微信小程序开发教程(三)项目目录及文件构成
  20. python IOError: windows directory not found at xxxxx win32

热门文章

  1. HASH算法具体解释
  2. Auto Layout深入理解,及masonry简单介绍
  3. php用两个栈来实现队列
  4. [Node.js] Create a model to persist data in a Node.js LoopBack API
  5. JavaEE 三层架构的浅谈
  6. Git使用经验小结
  7. RSA解密时javax.crypto.BadPaddingException: Data must start with zero
  8. USACO--2.1The Castle
  9. 在TMemo上画一条线(超级简单,举一反三)
  10. Linux中vim编辑器莫名下方出现H的问题