博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c# ini file
阅读量:7236 次
发布时间:2019-06-29

本文共 6550 字,大约阅读时间需要 21 分钟。

ini文件主要用于保存配置。之前一直以为是当作普通文本进行操作,读取里面的内容,再自己解析读取的文本。后来发现已经有写好的api函数:WritePrivateProfileString()和GetPrivateProfileString()。

然后查看怎么使用,直到看到有人已经封装成了类,直接使用即可。代码源自一下链接,稍微做了下笔记,记录于此。

参考链接:

  

  

IniFile.cs

using System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices;namespace INIFILE{    public abstract class CustomIniFile    {        public CustomIniFile(string AFileName)        {            FFileName = AFileName;        }        private string FFileName;        public string FileName        {            get { return FFileName; }        }        public virtual bool SectionExists(string Section)        {            List
vStrings = new List
(); ReadSections(vStrings); return vStrings.Contains(Section); } public virtual bool ValueExists(string Section, string Ident) { List
vStrings = new List
(); ReadSection(Section, vStrings); return vStrings.Contains(Ident); } public abstract string ReadString(string Section, string Ident, string Default); public abstract bool WriteString(string Section, string Ident, string Value); public abstract bool ReadSectionValues(string Section, List
Strings); public abstract bool ReadSection(string Section, List
Strings); public abstract bool ReadSections(List
Strings); public abstract bool EraseSection(string Section); public abstract bool DeleteKey(string Section, string Ident); public abstract bool UpdateFile(); } ///
/// 存储本地INI文件的类。 /// public class IniFile : CustomIniFile { [DllImport("kernel32")] private static extern uint GetPrivateProfileString( string lpAppName, // points to section name string lpKeyName, // points to key name string lpDefault, // points to default string byte[] lpReturnedString, // points to destination buffer uint nSize, // size of destination buffer string lpFileName // points to initialization filename ); [DllImport("kernel32")] private static extern bool WritePrivateProfileString( string lpAppName, // pointer to section name string lpKeyName, // pointer to key name string lpString, // pointer to string to add string lpFileName // pointer to initialization filename ); ///
/// 构造IniFile实例。 /// 指定文件名 /// public IniFile(string AFileName) : base(AFileName) { } ///
/// 析够IniFile实例。 /// ~IniFile() { UpdateFile(); } ///
/// 读取字符串值。 /// 指定变量标识。 /// 指定所在区域。 /// 指定默认值。 ///
返回读取的字符串。如果读取失败则返回该值。
///
/* 读取key对应的value的值,如果对应的key不存在或者存在,但没有等号,就是用默认值Default */ public override string ReadString(string Section, string Ident, string Default) { byte[] vBuffer = new byte[2048]; uint vCount = GetPrivateProfileString(Section, Ident, Default, vBuffer, (uint)vBuffer.Length, FileName); return Encoding.Default.GetString(vBuffer, 0, (int)vCount); } ///
/// 写入字符串值。 /// ///
指定所在区域。 ///
指定变量标识。 ///
所要写入的变量值。 ///
返回写入是否成功。
public override bool WriteString(string Section, string Ident, string Value) { return WritePrivateProfileString(Section, Ident, Value, FileName); } ///
/// 获得区域的完整文本。(变量名=值格式)。 /// ///
指定区域标识。 ///
输出处理结果。 ///
返回读取是否成功。
public override bool ReadSectionValues(string Section, List
Strings) { List
vIdentList = new List
(); // 先读取某个section中所有的key值 if (!ReadSection(Section, vIdentList)) return false; // 根据key值获取value的值,并按照格式组成string foreach (string vIdent in vIdentList) Strings.Add(string.Format("{0}={1}", vIdent, ReadString(Section, vIdent, ""))); return true; } ///
/// 读取区域变量名列表。 /// ///
指定区域名。 ///
指定输出列表。 ///
返回获取是否成功。
/* 读取某一个section中所有的key的值*/ public override bool ReadSection(string Section, List
Strings) { byte[] vBuffer = new byte[16384]; //读取整个section中的key值,存到vBuffer中 uint vLength = GetPrivateProfileString(Section, null, null, vBuffer, (uint)vBuffer.Length, FileName); int j = 0; for (int i = 0; i < vLength; i++) if (vBuffer[i] == 0) { //将byte类型转成string,并保存到动态数组中, 按照0分开 //byte内容如下,每个key通过00分隔 //4E-61-6D-65-00-42-61-75-64-52-61-74-65-00-44-61-74-61-42-69-74-73-00-53-74-6F-70-42-69-74-73-00-47-5F-50-41-52-49-54-59 //将一长串字符组成string,就是将2个00之间的byte内容组成string,并添加到动态数组中 Strings.Add(Encoding.Default.GetString(vBuffer, j, i - j)); j = i + 1; } return true; } ///
/// 读取区域名列表。 /// ///
指定输出列表。 ///
/* 读取的是中括号中的section名字 */ public override bool ReadSections(List
Strings) { byte[] vBuffer = new byte[16384]; uint vLength = GetPrivateProfileString(null, null, null, vBuffer, (uint)vBuffer.Length, FileName); int j = 0; for (int i = 0; i < vLength; i++) if (vBuffer[i] == 0) { // 将数组中第j~i个byte的内容转成string,添加到list中 Strings.Add(Encoding.Default.GetString(vBuffer, j, i - j)); j = i + 1; } return true; } ///
/// 删除指定区域。 /// ///
指定区域名。 ///
返回删除是否成功。
/* 删除整个section的内容 */ public override bool EraseSection(string Section) { return WritePrivateProfileString(Section, null, null, FileName); } ///
/// 删除指定变量。 /// ///
变量所在区域。 ///
变量标识。 ///
返回删除是否成功。
/* 删除之后,key和value的值都不存在 */ public override bool DeleteKey(string Section, string Ident) { return WritePrivateProfileString(Section, Ident, null, FileName); } ///
/// 更新文件。 /// ///
返回更新是否成功。
public override bool UpdateFile() { return WritePrivateProfileString(null, null, null, FileName); } }}

Profile.cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace INIFILE{    class Profile    {        public static void LoadProfile()        {            //获取生成的可执行文件的目录            string strPath = AppDomain.CurrentDomain.BaseDirectory;            _file = new IniFile(strPath + "Cfg.ini");            G_BAUDRATE = _file.ReadString("CONFIG", "BaudRate", "4800");    //读数据,下同            G_DATABITS = _file.ReadString("CONFIG", "DataBits", "8");            G_STOP = _file.ReadString("CONFIG", "StopBits", "1");            G_PARITY = _file.ReadString("CONFIG", "Parity", "NONE");        }        public static void SaveProfile()        {            string strPath = AppDomain.CurrentDomain.BaseDirectory;            _file = new IniFile(strPath + "Cfg.ini");            _file.WriteString("CONFIG", "BaudRate", G_BAUDRATE);            //写数据,下同            _file.WriteString("CONFIG", "DataBits", G_DATABITS);            _file.WriteString("CONFIG", "StopBits", G_STOP);            _file.WriteString("CONFIG", "G_PARITY", G_PARITY);        }        private static IniFile _file;//内置了一个对象        public static string G_BAUDRATE = "1200";//给ini文件赋新值,并且影响界面下拉框的显示        public static string G_DATABITS = "8";        public static string G_STOP = "1";        public static string G_PARITY = "NONE";    }}

Tony Liu

2016-9-26,Shenzhen

转载地址:http://lnlfm.baihongyu.com/

你可能感兴趣的文章
C++一个简单的手柄类模板
查看>>
The Water Problem(排序)
查看>>
atitit.无线上网卡 无法搜索WiFi 解决无线路由器信号不能被连接
查看>>
C#进阶系列——DDD领域驱动设计初探(三):仓储Repository(下)
查看>>
android 电容屏(三):驱动调试之驱动程序分析篇
查看>>
数字签名时间戳服务器的原理 !
查看>>
C++ Split string into vector<string> by space
查看>>
JavaScript学习——内置属性
查看>>
Oracle Profile 使用详解--zhuanzai
查看>>
Hadoop-1.1.2、HBase-0.94.7完全分布式集群结构
查看>>
TP-Link wr703N 使用华为HiLink系列上网卡的设置【转】
查看>>
士兵杀敌(四)(树状数组+线段树)
查看>>
Linux 高可用(HA)集群基本概念2
查看>>
Struts+Spring+Hibernate整合入门详解
查看>>
[转载]浅谈组策略设置IE受信任站点
查看>>
【转】maven导出项目依赖的jar包
查看>>
JS实现文本复制与剪切
查看>>
s标签可以if elseif else
查看>>
每天一个linux命令(20):linux chmod命令
查看>>
MySQL复合分区
查看>>