【发布时间】:2016-08-31 21:03:15
【问题描述】:
我有一个 INI 文件,它由多个部分和一个名为“路径”的键组成。 INI 中的所有内容都在加载时加载到 DataGridView 中,用于操作文件的内容。
INI Example:
[First Entry]
Path=C:\test1.txt
[Second Entry]
Path=C:\test2.txt
[Third Entry]
Path=C:\test3.text
删除 [Second Entry] 且不会清除整个文件的最简单方法是什么?
这是我目前正在使用的将新信息写入文件的内容:
INI Class:
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string fileName);
public void Write(string section, string key, string value)
{
WritePrivateProfileString(section, key, value.ToLower(), path);
}
Form Button:
private void WriteINI()
{
myINI.Write(txtName.Text, "Path", txtPath.Text);
ReadINI();
}
【问题讨论】:
-
轻松转换为 C#:stackoverflow.com/a/31671066/3740093
-
加载到数据结构中,只写回你想保留的字段。
-
您是否考虑过使用库来处理 ini 文件?我创建了一个:github.com/rickyah/ini-parser(可以用 NuGet 安装)