C# 通过资源文件(txt)将数据保存在SortedDictionary中

C# 通过资源文件(txt)将数据保存在SortedDictionary中,c#,.net,dictionary,C#,.net,Dictionary,我在资源文件(*.resx)中保存了一个txt文件,其中包含一些信息: 23,TRUNK-1,Trunk-1,,[Barry_Boehm] 24,TRUNK-2,Trunk-2,,[Barry_Boehm] 25,LEAF-1,Leaf-1,,[Barry_Boehm] 26,LEAF-2,Leaf-2,,[Barry_Boehm] 136,UDPLite,,,[RFC3828] 。。。并希望将第一个和第二个条目保存到SortedDic

我在资源文件(*.resx)中保存了一个txt文件,其中包含一些信息:

    23,TRUNK-1,Trunk-1,,[Barry_Boehm]   
    24,TRUNK-2,Trunk-2,,[Barry_Boehm]   
    25,LEAF-1,Leaf-1,,[Barry_Boehm] 
    26,LEAF-2,Leaf-2,,[Barry_Boehm] 
    136,UDPLite,,,[RFC3828] 
。。。并希望将第一个和第二个条目保存到SortedDictionary中:

    23,TRUNK-1  
    24,TRUNK-2  
    25,LEAF-1
    26,LEAF-2
    136,UDPLite

publicstaticsorteddictionary xTypes=newsorteddictionary();
String[]rows=Regex.Split(Resources.ProTypes.ProTypesSource,“\r\n”);
foreach(行中的变量i)
{
String[]words=i.Split(新[]{',});
...
添加(proNumber,proName);
}

我该怎么做呢?

你可以这样做:

        SortedDictionary<UInt16, string> xTypes = new SortedDictionary<UInt16, string>();

        String[] rows = Regex.Split("23,TRUNK-1,Trunk-1,,[Barry_Boehm]", "\r\n");

        foreach (var i in rows)
        {
            String[] words = i.Split(new[] { ',' });

            UInt16 proNumber = Convert.ToUInt16(words[0]);
            string proName = words[1];

            xTypes.Add(proNumber, proName);
        }
SortedDictionary xTypes=新的SortedDictionary();
String[]rows=Regex.Split(“23,TRUNK-1,TRUNK-1,,[Barry_Boehm]”,“\r\n”);
foreach(行中的变量i)
{
String[]words=i.Split(新[]{',});
UInt16 proNumber=Convert.ToUInt16(单词[0]);
字符串proName=单词[1];
添加(proNumber,proName);
}

您似乎已经完成了几乎所有的工作:

foreach (var i in rows)
{
    String[] words = i.Split(new[] { ',' });

    UInt16 proNumber= UInt16.Parse(words[0]);
    string  proName=words[1];

    xTypes.Add(proNumber, proName);
}
publicstaticsorteddictionary xTypes=newsorteddictionary();
String[]rows=Regex.Split(Resources.ProTypes.ProTypesSource,“\r\n”);
foreach(行中的变量i){
String[]words=i.Split(新[]{',});
Add(UInt16.Parse(words[0]),words[1]);
}

xTypes.Add(UInt16.Parse(words[0]),words[1])?我想可能有更好/更快的解决方案。谢谢大家:)
foreach (var i in rows)
{
    String[] words = i.Split(new[] { ',' });

    UInt16 proNumber= UInt16.Parse(words[0]);
    string  proName=words[1];

    xTypes.Add(proNumber, proName);
}
public static SortedDictionary<UInt16, string> xTypes = new SortedDictionary<UInt16, string>();

String[] rows = Regex.Split(Resources.ProTypes.ProTypesSource, "\r\n");

foreach (var i in rows){
    String[] words = i.Split(new[] { ',' });
    xTypes.Add(UInt16.Parse(words[0]), words[1]);
}