介绍C#如何对ini文件进行读写操做,C#能够经过调用【kernel32.dll】文件中的 WritePrivateProfileString()和GetPrivateProfileString()函数分别对ini文件进行读和写操做。包括:读取key的值、保存key的值、读取全部section、读取全部key、移除section、移除key等操做。html
1. ini文件介绍web
2. 读取操做:包括读取key的值、读取全部section、读取全部key等操做。app
3. 写入操做: 包括保存key的值、移除section、移除key等操做。函数
4. 源码下载:展现运行图及源码下载spa
ini文件经常使用于存储各种应用的配置信息,而内部的文件结构主要包括三个概念:section、key和value。.net
其中section为各独立的区域块,名称能够为英文、中文。htm
C#能够经过调用【kernel32.dll】文件中的 GetPrivateProfileString()函数对ini文件进行读取操做。blog
官方API:https://msdn.microsoft.com/zh-cn/library/ms724353.aspxip
函数签名:rem
[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的值。
/// <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; }
注意:中文名称的section要进行转码。
/// <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(); }
一样要对中问名称的key进行转码。
/// <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(); }
C#能够经过调用【kernel32.dll】文件中的 WritePrivateProfileString()函数对ini文件进行写入操做。
官方API:https://msdn.microsoft.com/zh-cn/library/ms725501.aspx
函数签名:
[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。
注意:若此key不存在将会建立,不然就为修改此key的值。
/// <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; }
说明:key参数传入null就为移除指定的section。
/// <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; }
说明:value参数传入null就为移除指定的key。
/// <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; }
百度网盘:http://pan.baidu.com/s/1dEQ3QuP
CSDN:http://download.csdn.net/detail/polk6/9684148