C# 配置文件(INI Config)

这篇博客讨论了在XML盛行的时代,作者对于INI配置文件的偏好。尽管XML提供了清晰的结构,但在性能上不如INI文件。文章提供了一个.NET风格的C# INI配置文件类,包括加载、保存、创建、删除等操作,使得使用INI文件变得简单。
摘要由CSDN通过智能技术生成
在这个漫天都是XML配置文件飞的年代很难在找我们这些还喜欢用INI/INF/CFG配置文件的人了
有时候真觉得让人感伤XML的性能是无法比拟INI配置文件的 但XML的树形结构直观可析 不过其
实INI配置文件同样可以建立为树形结构本身它便是树形结构 有人可能说INI不安全 那么我请问你
XML又安全在哪里了呢 还不是一个鬼样 XML除结构清晰层次分明 但在性能上它与INI比不占优势
推崇它可以 但别把它和INI结构配置文件进行比较 它们本身就是不一样的东西

下面的代码按照.Net的风格编写的一套INI配置文件类 想必大家应该可以轻松上手 函数名你会发
现会有些XmlDocument的命名然而它们的用法本质都一样 Load加载 / Save 保存

using System;
using System.Collections;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace Configuration
{
    internal class Win32Native
    {
        [DllImport("Kernel32.dll", EntryPoint = "GetPrivateProfileSectionNamesA")]
        public static extern int GetPrivateProfileSectionNames([Out, MarshalAs(UnmanagedType.LPArray)] char[] lpszReturnBuffer, int nSize, [In, MarshalAs(UnmanagedType.LPStr)] string lpFileName);

        [DllImport("Kernel32.dll", EntryPoint = "GetPrivateProfileSectionA")]
        public static extern int GetPrivateProfileSection(string lpAppName, [MarshalAs(UnmanagedType.VBByRefStr)]ref string lpReturnedString, int nSize, string lpFileName);

        [DllImport("Kernel32.dll", EntryPoint = "WritePrivateProfileStringA")]
        public static extern int WritePrivateProfileString(string section, string key, string value, string files);
    }


    public class Profile // ini
    {

        public string FileName 
        { 

            get;
            set;
        }

        public Profile()
        {
            this.Sections = new SectionCollection(this);
        }

        public Profile(string filename) : this()
        {
            this.FileName = filename;
        }

        public void Load()
        {

            int len; char[] buffer;
            if ((len = this.Length) > 0)
     
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于INI文件的操作,可以进行封装,方便代码复用,具体实现如下: 1. 创建一个类 ```csharp public class IniHelper { [DllImport("kernel32", CharSet = CharSet.Unicode)] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); [DllImport("kernel32", CharSet = CharSet.Unicode)] private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); public static string ReadIni(string iniFilePath, string section, string key, string defaultValue = "") { int size = 255; // 缓冲区大小 StringBuilder sb = new StringBuilder(size); GetPrivateProfileString(section, key, defaultValue, sb, size, iniFilePath); return sb.ToString(); // 获取键值 } public static void WriteIni(string iniFilePath, string section, string key, string value) { WritePrivateProfileString(section, key, value, iniFilePath); } } ``` 2. 调用封装好的方法进行读写操作 ```csharp string iniFilePath = @"C:\Config\MyConfig.ini"; // 配置文件路径 string section = "SectionName"; // 节点名称 string key = "KeyName"; // 键名称 string defaultValue = "DefaultValue"; // 默认值 // 读取配置项 string value = IniHelper.ReadIni(iniFilePath, section, key, defaultValue); // 写入配置项 string valueToWrite = "MyValue"; IniHelper.WriteIni(iniFilePath, section, key, valueToWrite); ``` 通过封装,可以将INI文件操作的通用方法提取出来,方便代码复用,减少代码重复。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值