2014-08-28 92 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); 
}