C# WPF 读取 INI 文件:示例代码和解析方法
可以使用 System.IO 命名空间下的 StreamReader 类来读取 INI 文件中的内容,具体代码如下:
string path = 'ini文件路径';
string section = '写日志';
List
using (StreamReader sr = new StreamReader(path)) { string line; while ((line = sr.ReadLine()) != null) { if (line.StartsWith('[') && line.EndsWith(']')) { section = line.Substring(1, line.Length - 2); } else if (section == '写日志') { string[] parts = line.Split('='); if (parts.Length == 2) { values.Add(parts[1]); } } } }
foreach (string value in values) { // 处理读取到的内容 }
在代码中,首先定义了 INI 文件的路径和需要读取的节 (section),然后使用 StreamReader 类逐行读取文件内容。如果当前行是节名,则更新 section 变量;如果当前节是需要读取的节,则解析出每一行的键值对,并将值添加到 values 列表中。最后遍历 values 列表,对读取到的内容进行处理。
原文地址: https://www.cveoy.top/t/topic/kdRn 著作权归作者所有。请勿转载和采集!