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(节点, 键, 默认值, 文件路径);

posted @ 2026-04-05 01:02  菜鸟的奋斗军  阅读(2)  评论(0)    收藏  举报