ini读写

简介: 引用:http://www.cnblogs.com/xugang/archive/2012/03/21/2409711.html INI 配置文件的格式    在早期的Windows 桌面系统中,主要是用INI 文件作为系统的配置文件,从Win95 以后开始转向使用注册表,但是还有很多系统配置是使用INI 文件的。

引用:http://www.cnblogs.com/xugang/archive/2012/03/21/2409711.html

INI 配置文件的格式   

在早期的Windows 桌面系统中,主要是用INI 文件作为系统的配置文件,从Win95 以后开始转向使用注册表,但是还有很多系统配置是使用INI 文件的。其实,INI 文件就是简单的text 文件,只不过这种txt 文件要遵循一定的INI 文件格式。

“.ini” 就是英文 “initialization” 的头三个字母的缩写;当然INI file 的后缀名也不一定是".ini"也可以是".cfg",".conf ”或者是".txt"。

经典格式:

INI文件的格式很简单,最基本的三个要素是:parameters,sections 和 comments。

什么是 parameters?

INI所包含的最基本的“元素”就是parameter;每一个parameter都有一个name和一个value,name和value是由等号“=”隔开。name在等号的左边。

如:  name = value

什么是sections ?

所有的parameters都是以sections为单位结合在一起的。所有的section名称都是独占一行,并且sections名字都被方括号包围着 ([ and ])。在section声明后的所有parameters都是属于该section。对于一个section没有明显的结束标志符,一个section的 开始就是上一个section的结束,或者是end of the file。Sections一般情况下不能被nested,当然特殊情况下也可以实现sections的嵌套。

section 如:   [section]

 什么是 comments ?

在INI 文件中注释语句是以分号“;”开始的。所有的注释语句不管多长都是独占一行直到结束的。在分号和行结束符之间的所有内容都是被忽略的。

注释如:   ;comments text

 

当然,上面讲的都是最经典的INI文件格式,随着使用的需求INI文件的格式也出现了很多变种;

变种格式:请参考:http://en.wikipedia.org/wiki/INI_file

 

我的 INI 配置文件读写动态库

其实就是调用了kernel32.dll 中的 WritePrivateProfileString 和 GetPrivateProfileString 函数。

kernel32.dll是Windows 9x/Me 中非常重要的32位动态链接库文件,属于内核级文件。它控制着系统的内存管理、数据的输入输出操作和中断处理。
 
INI 配置文件读写动态库 INIHelper.dll 的源码很简单,代码如下:
复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;

namespace INIHelper
{
public class INIFileHelper
{
private string strFileName = ""; //INI文件名
private string strFilePath = "";//获取INI文件路径

public INIFileHelper()
{
strFileName = "Config.ini"; //INI文件名
//方法1获取INI文件路径
strFilePath = Directory.GetCurrentDirectory() + "\\" + strFileName;
//方法2:获取INI文件路径
//strFilePath = Path.GetFullPath(".\\") + strFileName;
}

public INIFileHelper(string FileName)
{
strFileName = FileName; //INI文件名
//
获取INI文件路径
strFilePath = Directory.GetCurrentDirectory() + "\\" + strFileName;
}

public INIFileHelper(string FullPath, string FileName)
{
strFileName = FileName; //INI文件名
strFilePath = FullPath + "\\" + strFileName;//获取INI文件路径
}

/// <summary>
/// 写入INI文件
/// </summary>
/// <param name="section">节点名称[如[TypeName]]</param>
/// <param name="key"></param>
/// <param name="val"></param>
/// <param name="filepath">文件路径</param>
/// <returns></returns>
[DllImport("kernel32")]
public static extern long WritePrivateProfileString(string section, string key, string val, string filepath);

/// <summary>
/// 写入
/// </summary>
/// <param name="sectionName">section 节点名称</param>
/// <param name="key">key 值</param>
/// <param name="value">value 值</param>
public void Write(string sectionName, string key, string value)
{
try
{
//根据INI文件名设置要写入INI文件的节点名称
//此处的节点名称完全可以根据实际需要进行配置
strFileName = Path.GetFileNameWithoutExtension(strFilePath);
INIFileHelper.WritePrivateProfileString(sectionName, key, value, strFilePath);
}
catch
{
throw new Exception("配置文件不存在或权限不足导致无法写入");
}
}

/// <summary>
/// 写入默认节点"FileConfig"下的相关数据
/// </summary>
/// <param name="key">key 值</param>
/// <param name="value">value 值</param>
public void Write(string key, string value)
{
// section 节点名称使用默认值:"FileConfig"
Write("FileConfig", key, value);
}

/// <summary>
/// 读取INI文件
/// </summary>
/// <param name="section">节点名称</param>
/// <param name="key"></param>
/// <param name="def"></param>
/// <param name="retval">stringbulider对象</param>
/// <param name="size">字节大小</param>
/// <param name="filePath">文件路径</param>
/// <returns></returns>
[DllImport("kernel32")]
public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath);

/// <summary>
/// 读取
/// </summary>
/// <param name="sectionName">section 节点名称</param>
/// <param name="key">key 值</param>
/// <returns>value 值</returns>
public string Read(string sectionName, string key)
{
if (File.Exists(strFilePath)) //读取时先要判读INI文件是否存在
{
strFileName = Path.GetFileNameWithoutExtension(strFilePath);
//return ContentValue(strFileName, key);
StringBuilder outValue = new StringBuilder(1024);
INIFileHelper.GetPrivateProfileString(sectionName, key, "", outValue, 1024, strFilePath);
return outValue.ToString();
}
else
{
throw new Exception("配置文件不存在");
}
}

/// <summary>
/// 读取默认节点"FileConfig"下的相关数据
/// </summary>
/// <param name="key">key 值</param>
/// <returns>value 值</returns>
public string Read(string key)
{
// section 节点名称使用默认值:"FileConfig"
return Read("FileConfig", key);
}

}
}
复制代码
 

对 INIHelper.dll 动态库的使用和测试,代码如下:

复制代码
// 获取INI文件路径
//  private string strFilePath = Application.StartupPath + "\\FileConfig.ini";

// 写入
private  void button1_Click( object sender, EventArgs e)
{
     // test1(WinForm 测试)
     string strFilePath =  " Config.ini "; // 获取INI文件路径
    INIFileHelper file1 =  new INIFileHelper(strFilePath);
    file1.Write(label1.Text, textBox1.Text);
    file1.Write(label2.Text, textBox2.Text);
    MessageBox.Show( " test1 写入完毕 ");

     // test2
    INIFileHelper file2 =  new INIFileHelper();
    file2.Write( " xugang "" http://xugang.cnblogs.com ");
    file2.Write( " hobby "" @#$%^&*() ");
    MessageBox.Show( " test2 写入完毕 ");

     // test3
    INIFileHelper file3 =  new INIFileHelper( " newConfig.ini ");
    file3.Write( " NewSection "" xugang "" http://xugang.cnblogs.com ");
    file3.Write( " NewSection "" hobby "" @#$%^&*() ");
    MessageBox.Show( " test3 写入完毕 ");

     // test4
     string strPath = Application.StartupPath;  // 文件路径
     string strName =  " xxx.ini "; // INI文件名称

    INIFileHelper file4 =  new INIFileHelper(strPath, strName);
    file4.Write( " NewSection "" xugang "" http://xugang.cnblogs.com ");
    file4.Write( " NewSection "" hobby "" @#$%^&*() ");
    MessageBox.Show( " test4 写入完毕 ");
}

// 读取
private  void button2_Click( object sender, EventArgs e)
{
     // test1(WinForm 测试)
     string strFilePath =  " Config.ini "; // 获取INI文件路径
    INIFileHelper file1 =  new INIFileHelper(strFilePath);
    StringBuilder str =  new StringBuilder();
    str.AppendLine(file1.Read(label1.Text));
    str.AppendLine(file1.Read(label2.Text));
    MessageBox.Show(str.ToString());

     // test2
    INIFileHelper file2 =  new INIFileHelper();
    MessageBox.Show(file2.Read( " xugang ") + "     "+file2.Read( " hobby "));

     // test3
    INIFileHelper file3 =  new INIFileHelper( " newConfig.ini ");
    MessageBox.Show( file3.Read( " NewSection "" xugang ")
           +  "     " + file3.Read( " NewSection "" hobby "));

     // test4
     string strPath = Application.StartupPath;  // 文件路径
     string strName =  " xxx.ini "; // INI文件名称

    INIFileHelper file4 =  new INIFileHelper(strPath, strName);
    MessageBox.Show(file4.Read( " NewSection "" xugang ")
          +  "     " + file4.Read( " NewSection "" hobby "));
}
复制代码
 

参考来源:

INI 配置文件的格式

INI 格式文件操作

 

源码下载

相关文章
|
5天前
|
人工智能 JSON 供应链
畅用7个月无影 JVS Claw |手把手教你把JVS改造成「科研与产业地理情报可视化大师」
LucianaiB分享零成本畅用JVS Claw教程(学生认证享7个月使用权),并开源GeoMind项目——将JVS改造为科研与产业地理情报可视化AI助手,支持飞书文档解析、地理编码与腾讯地图可视化,助力产业关系图谱构建。
23324 5
畅用7个月无影 JVS Claw |手把手教你把JVS改造成「科研与产业地理情报可视化大师」
|
14天前
|
缓存 人工智能 自然语言处理
我对比了8个Claude API中转站,踩了不少坑,总结给你
本文是个人开发者耗时1周实测的8大Claude中转平台横向评测,聚焦Claude Code真实体验:以加权均价(¥/M token)、内部汇率、缓存支持、模型真实性及稳定性为核心指标。
5174 25
|
10天前
|
人工智能 JSON BI
DeepSeek V4 来了!超越 Claude Sonnet 4.5,赶紧对接 Claude Code 体验一把
JeecgBoot AI专题研究 把 Claude Code 接入 DeepSeek V4Pro 的真实体验与避坑记录 本文记录我将 Claude Code 对接 DeepSeek 最新模型(V4Pro)后的真实体验,测试了 Skills 自动化查询和积木报表 AI 建表两个场景——有惊喜,也踩
3686 12
|
9天前
|
人工智能 缓存 BI
Claude Code + DeepSeek V4-Pro 真实评测:除了贵,没别的毛病
JeecgBoot AI专题研究 把 Claude Code 接入 DeepSeek V4Pro,跑完 Skills —— OA 审批、大屏、报表、部署 5 大实战场景后的真实体验 ![](https://oscimg.oschina.net/oscnet/up608d34aeb6bafc47f
3027 10
Claude Code + DeepSeek V4-Pro 真实评测:除了贵,没别的毛病
|
26天前
|
人工智能 自然语言处理 安全
Claude Code 全攻略:命令大全 + 实战工作流(建议收藏)
本文介绍了Claude Code终端AI助手的使用指南,主要内容包括:1)常用命令如版本查看、项目启动和更新;2)三种工作模式切换及界面说明;3)核心功能指令速查表,包含初始化、压缩对话、清除历史等操作;4)详细解析了/init、/help、/clear、/compact、/memory等关键命令的使用场景和语法。文章通过丰富的界面截图和场景示例,帮助开发者快速掌握如何通过命令行和交互界面高效使用Claude Code进行项目开发,特别强调了CLAUDE.md文件作为项目知识库的核心作用。
20963 63
Claude Code 全攻略:命令大全 + 实战工作流(建议收藏)