<分区>
我发现应用程序使用 .txt 文件作为配置文件。
文件看起来像
[[plugin.save]]=[[保存]]
我如何在 C# 中使用它?
如何将文件读取到我的应用程序以使用 [] 中的值作为配置?
标签 c# configuration
<分区>
我发现应用程序使用 .txt 文件作为配置文件。
文件看起来像
[[plugin.save]]=[[保存]]
我如何在 C# 中使用它?
如何将文件读取到我的应用程序以使用 [] 中的值作为配置?
最佳答案
您必须将其作为常规文件来阅读。阅读它使用字典来存储值。 示例代码:
Dictionary<string, string> configuration = new Dictionary<string, string>();
Regex r = new Regex(@"\[\[(\w+)\]\]=\[\[(\w+)\]\]");
string[] configArray = {"[[param1]]=[[Value1]]", "[[param2]]=[[Value2]]"};// File.ReadAllLines("some.txt");
foreach (string config in configArray)
{
Match m = r.Match(config);
configuration.Add(m.Groups[1].Value, m.Groups[2].Value);
}
请注意检查可能的空值。 另请注意,如果配置值可以包含例如空格,则正则表达式应该不同。
关于c# - txt文件作为配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36222408/