2014-08-28 84 views
1

我保存在一个资源文件中一个txt文件(*的.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 

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(proNumber, proName); 
} 

我怎么能这样做?

+2

'xTypes.Add(UInt16.Parse(字[0]),字[1]);'? – 2014-08-28 11:44:18

+0

我认为可能有更好/更快的解决方案。谢谢大家 :) – Stampy 2014-08-28 11:58:58

回答

1
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]); 
} 
1

你可以这样说:

 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); 
     } 
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); 
}