当前位置: 首页 > news >正文

C# INI = 最简单的配置文件

[设置]
名字=张三
年龄=18
开启=true

[路径]
软件路径=C:\test

[ ] 叫 节点(Section)
名字=张三 叫 键值对(Key=Value)
用来存:配置、参数、用户设置

using System;
using System.Runtime.InteropServices;

// INI 操作工具类(单独放,Main不能写在这里)
public class IniHelper
{
[DllImport("kernel32.dll")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

[DllImport("kernel32.dll")]
private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);// 读INI
public static string Read(string section, string key, string defValue, string iniPath)
{byte[] buffer = new byte[2048];GetPrivateProfileString(section, key, defValue, buffer, buffer.Length, iniPath);return System.Text.Encoding.Default.GetString(buffer).TrimEnd('\0');
}// 写INI
public static void Write(string section, string key, string value, string iniPath)
{WritePrivateProfileString(section, key, value, iniPath);
}

}

// 主程序类(Main必须在这里)
class Program
{
static void Main()
{
// 1. 定义INI文件路径(绝对路径,避免找不到)
string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.ini");
Console.WriteLine("INI文件将生成在:" + path);

    // 2. 写入配置IniHelper.Write("设置", "姓名", "小明", path);IniHelper.Write("设置", "年龄", "14", path);IniHelper.Write("设置", "班级", "高一三班", path);IniHelper.Write("路径", "缓存目录", "C:\\Cache", path);IniHelper.Write("配置", "自动启动", "True", path); // 修正:布尔值写True/False,不是"自动启动"IniHelper.Write("配置", "窗口宽度", "1000", path);IniHelper.Write("配置", "音量", "80", path);Console.WriteLine("✅ 写入INI成功!");// 3. 读取配置string name = IniHelper.Read("设置", "姓名", "未知", path);string age = IniHelper.Read("设置", "年龄", "0", path);string cache = IniHelper.Read("路径", "缓存目录", "无", path);bool autoStart = bool.Parse(IniHelper.Read("配置", "自动启动", "False", path));int width = int.Parse(IniHelper.Read("配置", "窗口宽度", "800", path));int volume = int.Parse(IniHelper.Read("配置", "音量", "50", path)); // 修正:键名统一为"音量"// 4. 输出结果Console.WriteLine("姓名:" + name);Console.WriteLine("年龄:" + age);Console.WriteLine("缓存目录:" + cache);Console.WriteLine("自动启动:" + autoStart);Console.WriteLine("窗口宽度:" + width);Console.WriteLine("音量:" + volume);// 5. 自动打开文件夹System.Diagnostics.Process.Start("explorer.exe", AppDomain.CurrentDomain.BaseDirectory);Console.WriteLine("\n按任意键退出...");Console.ReadKey();
}

}

——————————————————————————————————————————————————————————————————————————————————————————

为什么要创建 IniHelper 这个类?

因为 C# 本身没有自带读写 INI 文件的功能!
但是 Windows 系统自带读写 INI 的功能,藏在系统内核里。
所以:
我们必须写一个工具类,去调用 Windows 系统功能
这个工具类,就叫:
IniHelper
它的作用只有一句话:
专门帮我们 读 / 写 配置文件(INI)

Write → 把配置写入文件(保存)
Read → 从文件读取配置(加载)

using System.Runtime.InteropServices;
→ 调用 Windows 系统功能必须加这个

————————————————————————————————————————————————————————————————————————————

[DllImport("kernel32.dll")]
private static extern long WritePrivateProfileString(
string section,
string key,
string val,
string filePath
);

这是 Windows 系统自带的写入 INI 函数我们只是把它 “借过来用”。
kernel32.dll = Windows 系统内核文件
WritePrivateProfileString = 系统写 INI 的方法
我们自己没写逻辑,调用系统功能


[DllImport("kernel32.dll")]
private static extern int GetPrivateProfileString(
string section,
string key,
string def,
byte[] retVal,
int size,
string filePath
);

这是 Windows 系统自带的读取 INI 函数


public static string Read(
string section,
string key,
string defValue,
string iniPath
)
{
byte[] buffer = new byte[2048];
GetPrivateProfileString(section, key, defValue, buffer, buffer.Length, iniPath);
return System.Text.Encoding.Default.GetString(buffer).TrimEnd('\0');
}

给你读取 INI 配置用的!
逐行解释:
byte[] buffer = new byte[2048];→ 开一个 2048 长度的盒子,用来装读取到的文字
GetPrivateProfileString(...)→ 调用系统功能,真正去读文件
return ...GetString(buffer)...→ 把读取到的内容转成字符串还给你
.TrimEnd('\0')→ 去掉多余空字符,让内容干净


public static void Write(
string section,
string key,
string value,
string iniPath
)
{
WritePrivateProfileString(section, key, value, iniPath);
}

给你写入 INI 配置用的!
调用系统函数
把你给的:节点、键、值、路径
写入文件


你只需要会用就可以
IniHelper.Write(节点, 键, 值, 文件路径);
string 值 = IniHelper.Read(节点, 键, 默认值, 文件路径);

http://www.jsqmd.com/news/589112/

相关文章:

  • 爱站seo工具的网站诊断报告怎么看
  • STM32智能营养称系统开发全解析
  • 迷彩伪装目标检测数据集VOC+YOLO格式839张1类别
  • 车载Android系统开发全流程解析与技术实践指南
  • Android双网卡管理芯片适配
  • OpenClaw模型缓存优化:减少百川2-13B-4bits的重复计算开销
  • 2026年无机岩艺石服务商深度测评:5家优质厂商全方位解析 - 2026年企业推荐榜
  • OpenClaw多模型切换指南:Qwen3-4B与本地LLM混合调用
  • 迷彩伪装识别分割数据集labelme格式868张1类别
  • AVR与Cortex-M0超轻量FIFO优化实践
  • picoEEPROM:RP2040平台的类型安全EEPROM存储库
  • RK3588平台三路MCP2515 SPI CAN + 双网卡Linux系统适配深度解析
  • 主流开源协议解析与选择指南
  • 2026河南产业园区招商服务商深度测评:数据驱动下的企业选址新范式 - 2026年企业推荐榜
  • 如何在windows上的卸载Photoshop的Imagecreator插件
  • OpenClaw深度学习:千问3.5-9B模型微调实战
  • 云原生环境中的CI/CD最佳实践
  • OpenClaw备份策略:Qwen3-14B镜像环境快速迁移与恢复方案
  • Linux系统编程(六) ---- 数据库 SQLite3
  • 企业SEO优化与网站内容建设的关系是什么
  • 2026年徐州老房翻新市场深度解析:如何挑选靠谱团队与品牌服务商? - 2026年企业推荐榜
  • OPTIGA Trust X Arduino安全库深度解析
  • RTOS任务切换机制与触发时机详解
  • AI应用开发工程师(LLMAgent方向)技术深度解析与面试指南
  • 2026登封武术教育机构深度测评:如何为孩子选择文武兼修的成长平台? - 2026年企业推荐榜
  • HJ161 走一个大整数迷宫
  • 第26章 2020真题作文
  • M5Unit-DigiClock模块:基于I²C的即插即用数字时钟解决方案
  • 深入解析ROS应用开发:架构、算法、硬件集成与工程实践
  • C++ 与 向量化掩码(Masking):在 C++ 矢量化计算中利用硬件掩码寄存器处理循环边界的条件分支逻辑