C# 解析和存储配置文件中的值

C# 解析和存储配置文件中的值,c#,C#,我正在尝试读取.cfg文件 我已成功读取值并将其存储到列表中,然后存储到数据网格中 对我来说,最难的部分是这个“linekey.INT.option”,如下所示 linekey.6.label = Call Park linekey.6.line = 1 linekey.6.pickup_value = linekey.6.type = 10 linekey.6.value = 70 linekey.7.label = Park 1 linekey.7.line = 1 linekey.7.p

我正在尝试读取.cfg文件

我已成功读取值并将其存储到列表中,然后存储到数据网格中

对我来说,最难的部分是这个“linekey.INT.option”,如下所示

linekey.6.label = Call Park
linekey.6.line = 1
linekey.6.pickup_value = 
linekey.6.type = 10
linekey.6.value = 70
linekey.7.label = Park 1
linekey.7.line = 1
linekey.7.pickup_value = 71
linekey.7.type = 16
linekey.7.value = 71
linekey.8.label = Park 2
linekey.8.line = 1
linekey.8.pickup_value = 72
linekey.8.type = 16
linekey.8.value = 72
我读取配置文件并将所有行键添加到字典中

        string[] fileLines = File.ReadAllLines(filepath);
        StringBuilder sb = new StringBuilder();
        foreach (string line in fileLines)
        {
            sb.AppendLine(line);
        }

        tbConfigInput.Text = sb.ToString();

        Dictionary<string, string> getLines = new Dictionary<string, string>();

        foreach (string line in tbConfigInput.Lines)
        {
            if (line != "#!version:1.0.0.1")
            {
                if (line != "")
                {
                    if (line.Contains("linekey"))
                    {
                        string[] splitLine = line.Split('=');
                        getLines.Add(splitLine[0], splitLine[1]);
                    }
                }
            }
        }
一些建议将是一大幸事。我已经在这上面呆了差不多一整天了


非常感谢

为什么不做以下事情:

        DataTable dt = new DataTable();
        // define structure
        dt.Columns.Add("Column1");
        dt.Columns.Add("Column2");
        // ....
        dt.Columns.Add("ColumnN");
        // Add rows like this:
        dt.Rows.Add(new object[] { "Column1 value", "Column2 value", .. , "ColumnN value" });

更新:完整样本

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;

namespace MdeLoadTest
{
    class DataRow
    {
        public string Label { get; set; }
        public int Line { get; set; }
        public int PickupValue { get; set; }
        public int Type { get; set; }
        public int Value { get; set; }
    }
    class ParseData
    {
        private List<DataRow> rows;
        private int ReadSource(string file)
        {
            string[] fileLines = File.ReadAllLines(file);


            rows = new List<DataRow>();
            string fieldInfo, fieldValue, fieldName;
            int lineNumber, oldLineNumber;
            string[] splitLine;
            string[] fieldInfoParts;
            oldLineNumber = -1;
            DataRow row = null;
            foreach (string line in fileLines)
            {

                splitLine = line.Split('=');
                fieldInfo = splitLine[0];                
                fieldValue = splitLine.Count() >0? splitLine[1]:null;
                fieldInfoParts = fieldInfo.Split('.');

                lineNumber = int.Parse(fieldInfoParts[1]);
                fieldName = fieldInfoParts[2];

                if(lineNumber != oldLineNumber)
                {
                    rows.Add(row);
                    row = new DataRow();
                    oldLineNumber = lineNumber;
                }
                switch (fieldName)
                {
                    case "label":
                        row.Label = fieldValue;
                        break;
                    case "line":
                        row.Line = int.Parse(fieldValue);
                        break;
                    case "pickup_value":
                        row.PickupValue = int.Parse(fieldValue);
                        break;
                    case "type":
                        row.Type = int.Parse(fieldValue);
                        break;
                    case "value":
                        row.Value = int.Parse(fieldValue);
                        break;
                    default:
                        throw new Exception($"Unknown key:{fieldName}");
                }
            }
            if (oldLineNumber != -1)
            {
                rows.Add(row);
            }
            return rows.Count;
        }

        DataTable table;
        private void InitTable()
        {
            DataTable dt = new DataTable();
            // define structure
            dt.Columns.Add("Label",typeof(string));
            dt.Columns.Add("Line", typeof(int));
            dt.Columns.Add("PickupValue", typeof(int));
            dt.Columns.Add("Type", typeof(int));
            dt.Columns.Add("Value", typeof(int));
        }

        private void PopulateData ()
        {
            foreach (var row in rows)
            {
                table.Rows.Add(row);
            }
        }

        public DataTable Load(string sourceFile)
        {
            if (ReadSource(sourceFile) < 1)
                return null;
            InitTable();
            PopulateData();

            return table;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统数据;
使用System.IO;
使用System.Linq;
使用系统文本;
命名空间MdeLoadTest
{
类数据行
{
公共字符串标签{get;set;}
公共整数行{get;set;}
公共int PickupValue{get;set;}
公共int类型{get;set;}
公共int值{get;set;}
}
类解析数据
{
私有列表行;
私有int ReadSource(字符串文件)
{
string[]fileLines=File.ReadAllLines(文件);
行=新列表();
字符串fieldInfo、fieldValue、fieldName;
int lineNumber,oldLineNumber;
字符串[]分隔线;
字符串[]字段和零件;
oldLineNumber=-1;
DataRow行=null;
foreach(文件行中的字符串行)
{
splitLine=line.Split('=');
fieldInfo=分割线[0];
fieldValue=splitLine.Count()>0?splitLine[1]:null;
fieldInfoParts=fieldInfo.Split('.');
lineNumber=int.Parse(fieldInfoParts[1]);
fieldName=fieldInfoParts[2];
如果(行号!=旧行号)
{
行。添加(行);
行=新数据行();
oldLineNumber=行号;
}
交换机(字段名)
{
案例“标签”:
行。标签=字段值;
打破
案例“行”:
row.Line=int.Parse(fieldValue);
打破
案例“皮卡_值”:
row.PickupValue=int.Parse(fieldValue);
打破
案例“类型”:
row.Type=int.Parse(fieldValue);
打破
案例“值”:
row.Value=int.Parse(fieldValue);
打破
违约:
抛出新异常($“未知键:{fieldName}”);
}
}
如果(oldLineNumber!=-1)
{
行。添加(行);
}
返回行数;
}
数据表;
私有void InitTable()
{
DataTable dt=新的DataTable();
//定义结构
添加(“标签”,类型(字符串));
添加(“行”,类型(int));
添加(“PickupValue”,typeof(int));
添加(“类型”,类型(int));
添加(“值”,类型(int));
}
私有void PopulateData()
{
foreach(行中的变量行)
{
table.Rows.Add(行);
}
}
公共数据表加载(字符串源文件)
{
if(读源(源文件)<1)
返回null;
InitTable();
填充数据();
返回表;
}
}
}

我在下面编写了一个通用例程来解析您的配置。不用说,您将不得不添加边界条件检查并对其进行其他测试。这段代码演示了如何从config解析点格式键,并将其存储在适当的结构中,以便于以后检索

class Configuration {
    Dictionary<string, List<Dictionary<string, string>>> ParsedConfig = new Dictionary<string, List<Dictionary<string, string>>>();

    public Configuration(string fileName) {
        ParseConfig(File.ReadLines(fileName));
    }

    void ParseConfig(IEnumerable<string> lines) {
        foreach (string line in lines) {
            string[] splitLine = line.Split(new char[] { '=' }, 2);
            if (splitLine.Length != 2)
                continue;

            var cfgKey = splitLine[0].Trim();       // you probably want to get rid of trailing and leading spaces
            var cfgValue = splitLine[1].Trim();

            if (!cfgKey.Contains('.')) { // handle regular keys
                var singularList = new List<Dictionary<string, string>>();
                ParsedConfig[cfgKey] = singularList;
                singularList.Add(new Dictionary<string, string>());
                singularList[0][string.Empty] = cfgValue;
                continue;
            }


            var keyParts = cfgKey.Split(new char[] { '.' }, 3); // break down the dotted key
            if (keyParts.Length != 3)
                continue;


            if (!ParsedConfig.TryGetValue(keyParts[0], out var indexedConfigList))
                ParsedConfig[keyParts[0]] = indexedConfigList = new List<Dictionary<string, string>>();

            var index = int.Parse(keyParts[1]);

            while (indexedConfigList.Count <= index) // add array slots for all indexes
                indexedConfigList.Add(null);

            var indexedConfig = indexedConfigList[index];
            if (indexedConfig == null)
                indexedConfigList[index] = indexedConfig = new Dictionary<string, string>();

            indexedConfig[keyParts[2]] = cfgValue;
        }

    }

    public Dictionary<string, string> GetGroupedConfig(string key, int index) {
        if (ParsedConfig.TryGetValue(key, out var indexedConfigList) && indexedConfigList.Count > index)
            return indexedConfigList[index];

        return null;
    }

    public string GetConfigValue(string key) {
        string value = null;
        if (ParsedConfig.TryGetValue(key, out var indexedConfigList) && indexedConfigList.Count > 0)
            indexedConfigList[0].TryGetValue(string.Empty, out value);
        return value;
    }

}
类配置{
Dictionary ParsedConfig=新字典();
公共配置(字符串文件名){
ParseConfig(File.ReadLines(fileName));
}
void ParseConfig(IEnumerable行){
foreach(行中的字符串行){
string[]splitLine=line.Split(新字符[]{'='},2);
如果(splitLine.Length!=2)
继续;
var cfgKey=splitLine[0].Trim();//您可能想去掉尾随空格和前导空格
var cfgValue=splitLine[1]。Trim();
如果(!cfgKey.Contains('.'){//处理常规键
var singularList=新列表();
ParsedConfig[cfgKey]=singularList;
添加(新字典());
singularList[0][string.Empty]=cfgValue;
继续;
}
var keyParts=cfgKey.Split(新字符[]{.'},3);//分解虚线键
如果(keyParts.Length!=3)
继续;
if(!ParsedConfig.TryGetValue(keyParts[0],out var indexedConfigList))
ParsedConfig[keyParts[0]]=indexedConfigList=new List();
var index=int.Parse(keyParts[1]);
while(indexedConfigList.Count索引)
返回indexedConfigList[索引];
返回null;
}
公共字符串GetConfigValue(字符串键){
字符串值=null;
if(ParsedConfig.TryGetValue(key,out var indexedConfigList)&&indexedConfigList.Count>0)
indexedConfigList[0].TryGetValue(string.Empty,输出值);
返回值;
}
}
Get*
方法演示如何从解析的配置中提取值


现在,您可以调用
Get*
函数,并根据需要在
DataTable

中使用值。这个问题最好通过面向对象编程来解决。数组和DataTable作为中间步骤很好,但是
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;

namespace MdeLoadTest
{
    class DataRow
    {
        public string Label { get; set; }
        public int Line { get; set; }
        public int PickupValue { get; set; }
        public int Type { get; set; }
        public int Value { get; set; }
    }
    class ParseData
    {
        private List<DataRow> rows;
        private int ReadSource(string file)
        {
            string[] fileLines = File.ReadAllLines(file);


            rows = new List<DataRow>();
            string fieldInfo, fieldValue, fieldName;
            int lineNumber, oldLineNumber;
            string[] splitLine;
            string[] fieldInfoParts;
            oldLineNumber = -1;
            DataRow row = null;
            foreach (string line in fileLines)
            {

                splitLine = line.Split('=');
                fieldInfo = splitLine[0];                
                fieldValue = splitLine.Count() >0? splitLine[1]:null;
                fieldInfoParts = fieldInfo.Split('.');

                lineNumber = int.Parse(fieldInfoParts[1]);
                fieldName = fieldInfoParts[2];

                if(lineNumber != oldLineNumber)
                {
                    rows.Add(row);
                    row = new DataRow();
                    oldLineNumber = lineNumber;
                }
                switch (fieldName)
                {
                    case "label":
                        row.Label = fieldValue;
                        break;
                    case "line":
                        row.Line = int.Parse(fieldValue);
                        break;
                    case "pickup_value":
                        row.PickupValue = int.Parse(fieldValue);
                        break;
                    case "type":
                        row.Type = int.Parse(fieldValue);
                        break;
                    case "value":
                        row.Value = int.Parse(fieldValue);
                        break;
                    default:
                        throw new Exception($"Unknown key:{fieldName}");
                }
            }
            if (oldLineNumber != -1)
            {
                rows.Add(row);
            }
            return rows.Count;
        }

        DataTable table;
        private void InitTable()
        {
            DataTable dt = new DataTable();
            // define structure
            dt.Columns.Add("Label",typeof(string));
            dt.Columns.Add("Line", typeof(int));
            dt.Columns.Add("PickupValue", typeof(int));
            dt.Columns.Add("Type", typeof(int));
            dt.Columns.Add("Value", typeof(int));
        }

        private void PopulateData ()
        {
            foreach (var row in rows)
            {
                table.Rows.Add(row);
            }
        }

        public DataTable Load(string sourceFile)
        {
            if (ReadSource(sourceFile) < 1)
                return null;
            InitTable();
            PopulateData();

            return table;
        }
    }
}
class Configuration {
    Dictionary<string, List<Dictionary<string, string>>> ParsedConfig = new Dictionary<string, List<Dictionary<string, string>>>();

    public Configuration(string fileName) {
        ParseConfig(File.ReadLines(fileName));
    }

    void ParseConfig(IEnumerable<string> lines) {
        foreach (string line in lines) {
            string[] splitLine = line.Split(new char[] { '=' }, 2);
            if (splitLine.Length != 2)
                continue;

            var cfgKey = splitLine[0].Trim();       // you probably want to get rid of trailing and leading spaces
            var cfgValue = splitLine[1].Trim();

            if (!cfgKey.Contains('.')) { // handle regular keys
                var singularList = new List<Dictionary<string, string>>();
                ParsedConfig[cfgKey] = singularList;
                singularList.Add(new Dictionary<string, string>());
                singularList[0][string.Empty] = cfgValue;
                continue;
            }


            var keyParts = cfgKey.Split(new char[] { '.' }, 3); // break down the dotted key
            if (keyParts.Length != 3)
                continue;


            if (!ParsedConfig.TryGetValue(keyParts[0], out var indexedConfigList))
                ParsedConfig[keyParts[0]] = indexedConfigList = new List<Dictionary<string, string>>();

            var index = int.Parse(keyParts[1]);

            while (indexedConfigList.Count <= index) // add array slots for all indexes
                indexedConfigList.Add(null);

            var indexedConfig = indexedConfigList[index];
            if (indexedConfig == null)
                indexedConfigList[index] = indexedConfig = new Dictionary<string, string>();

            indexedConfig[keyParts[2]] = cfgValue;
        }

    }

    public Dictionary<string, string> GetGroupedConfig(string key, int index) {
        if (ParsedConfig.TryGetValue(key, out var indexedConfigList) && indexedConfigList.Count > index)
            return indexedConfigList[index];

        return null;
    }

    public string GetConfigValue(string key) {
        string value = null;
        if (ParsedConfig.TryGetValue(key, out var indexedConfigList) && indexedConfigList.Count > 0)
            indexedConfigList[0].TryGetValue(string.Empty, out value);
        return value;
    }

}