Ini文件的读写

最简单的Ini读写:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Text;

namespace AuxiliaryTools
{
    public static class IniHelper
    {
        /// <summary>
        /// 读 INI
        /// </summary>
        /// <param name="file">完整路径</param>
        /// <param name="section">段名,如 "UI"</param>
        /// <param name="key">键名,如 "Width"</param>
        /// <param name="defaultValue">找不到时返回什么</param>
        public static string Read(string file, string section, string key, string defaultValue = "")
        {
            if (!File.Exists(file)) return defaultValue;

            string target = $"[{section}]";          // [UI]
            bool hitSection = false;
            foreach (var line in File.ReadLines(file, Encoding.UTF8))
            {
                var s = line.Trim();
                if (s.StartsWith("[") && s.EndsWith("]"))
                {
                    hitSection = string.Equals(s, target, System.StringComparison.OrdinalIgnoreCase);
                    continue;
                }
                if (!hitSection) continue;

                var kv = s.Split(new[] { '=' }, 2);
                if (kv.Length == 2 &&
                    string.Equals(kv[0].Trim(), key, System.StringComparison.OrdinalIgnoreCase))
                    return kv[1].Trim();
            }
            return defaultValue;
        }

        /// <summary>
        /// 写 INI(无则创建,有则覆盖)
        /// </summary>
        public static void Write(string file, string section, string key, string value)
        {
            var lines = new List<string>();
            bool hitSection = false, done = false;

            if (File.Exists(file))
                lines.AddRange(File.ReadAllLines(file, Encoding.UTF8));

            string target = $"[{section}]";
            for (int i = 0; i < lines.Count; i++)
            {
                var s = lines[i].Trim();
                if (s.StartsWith("[") && s.EndsWith("]"))
                {
                    hitSection = string.Equals(s, target, System.StringComparison.OrdinalIgnoreCase);
                    continue;
                }
                if (!hitSection) continue;

                var kv = s.Split(new[] { '=' }, 2);
                if (kv.Length == 2 &&
                    string.Equals(kv[0].Trim(), key, System.StringComparison.OrdinalIgnoreCase))
                {
                    lines[i] = $"{key}={value}";   // 原地替换
                    done = true;
                    break;
                }
            }

            if (!done)   // 没找到,追加
            {
                if (!hitSection) lines.Add(target);
                lines.Add($"{key}={value}");
            }

            File.WriteAllLines(file, lines, Encoding.UTF8);
        }
    }
}

调用:

//
IniHelper.Write(ini, "Project_Color", "Color_SET_CS", value.ToString());
//
IniHelper.Read(ini, "Project_Color", "Color_SET_WWC", "")

 

posted @ 2025-09-25 10:12  Cmale  阅读(6)  评论(0)    收藏  举报