c# - txt文件作为配置文件

标签 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/

相关文章:

c# - 找不到某些 Boost 库函数

c# - WPF 工具包 TimePicker 值不会按时间手动输入更改,但会在焦点更改时触发

c# - LINQ to Entities 无法识别方法 "GetUserById"

c# - 检测 JIT 是否可用

Symfony2预定义参数

c# - 如何避免 SerializationException : Type is not resolved for member XXX when testing a component that uses the LogicalCallContext

c# - 从 Windows 身份验证中获取 `ICredentials`

configuration - HAProxy 的热重新配置仍然导致请求失败,有什么建议吗?

configuration - 在 application.conf 中使用相对路径作为配置值

c++ - QT多项目配置