cShap ini文件操作带注释
时间: 2023-10-04 18:06:27 浏览: 27
// 引入命名空间
using System.IO;
using System.Collections.Generic;
// 定义类
public class IniFile
{
// 定义私有变量
private string filePath; // 文件路径
private Dictionary<string, Dictionary<string, string>> sections; // 存储所有的Section和其下的键值对
// 构造函数
public IniFile(string filePath)
{
this.filePath = filePath;
this.sections = new Dictionary<string, Dictionary<string, string>>();
ReadIniFile();
}
// 读取ini文件
private void ReadIniFile()
{
if (!File.Exists(filePath)) return; // 如果文件不存在,则直接返回
string currentSection = ""; // 当前的Section
string line; // 存储每行数据的变量
using (StreamReader sr = new StreamReader(filePath)) // 使用StreamReader读取文件内容
{
while ((line = sr.ReadLine()) != null) // 逐行读取
{
line = line.Trim(); // 去除前后空格
if (line.StartsWith(";") || line.StartsWith("#")) continue; // 如果是注释,则跳过
if (line.StartsWith("[") && line.EndsWith("]")) // 如果是Section
{
currentSection = line.Substring(1, line.Length - 2); // 获取Section名称,去掉中括号
sections.Add(currentSection, new Dictionary<string, string>()); // 添加到字典中
}
else if (line.Contains("=")) // 如果是键值对
{
string[] keyValue = line.Split(new char[] { '=' }, 2); // 分割成键和值
string key = keyValue[0].Trim(); // 获取键,去除前后空格
string value = keyValue[1].Trim(); // 获取值,去除前后空格
if (currentSection == "") // 如果当前没有Section
{
currentSection = "Global"; // 将键值对添加到全局Section
if (!sections.ContainsKey(currentSection))
{
sections.Add(currentSection, new Dictionary<string, string>());
}
}
Dictionary<string, string> dict = sections[currentSection]; // 获取当前Section的字典
if (!dict.ContainsKey(key)) // 如果当前Section的字典中不存在该键,则添加
{
dict.Add(key, value);
}
}
}
}
}
// 写入ini文件
public void WriteIniFile()
{
using (StreamWriter sw = new StreamWriter(filePath))
{
foreach (KeyValuePair<string, Dictionary<string, string>> section in sections) // 遍历所有的Section
{
if (section.Key != "Global") // 如果不是全局Section,则写入中括号和Section名称
{
sw.WriteLine("[" + section.Key + "]");
}
foreach (KeyValuePair<string, string> keyValue in section.Value) // 遍历当前Section下的所有键值对
{
sw.WriteLine(keyValue.Key + " = " + keyValue.Value); // 写入键值对
}
sw.WriteLine(); // 写入空行
}
}
}
// 获取键值对的值
public string GetValue(string section, string key, string defaultValue = "")
{
if (sections.ContainsKey(section) && sections[section].ContainsKey(key)) // 如果Section和键都存在
{
return sections[section][key]; // 返回对应的值
}
return defaultValue; // 否则返回默认值
}
// 设置键值对的值
public void SetValue(string section, string key, string value)
{
if (!sections.ContainsKey(section)) // 如果Section不存在,则添加
{
sections.Add(section, new Dictionary<string, string>());
}
sections[section][key] = value; // 设置对应的值
}
// 删除键值对
public void DeleteValue(string section, string key)
{
if (sections.ContainsKey(section) && sections[section].ContainsKey(key)) // 如果Section和键都存在
{
sections[section].Remove(key); // 删除键值对
}
}
// 删除Section
public void DeleteSection(string section)
{
if (sections.ContainsKey(section)) // 如果Section存在
{
sections.Remove(section); // 删除Section
}
}
// 获取所有的Section名称
public List<string> GetSections()
{
return new List<string>(sections.Keys); // 返回字典中所有的Section名称
}
// 获取某个Section下的所有键名称
public List<string> GetKeys(string section)
{
if (sections.ContainsKey(section)) // 如果Section存在
{
return new List<string>(sections[section].Keys); // 返回该Section下的所有键名称
}
return new List<string>(); // 否则返回空列表
}
}