2011-12-23 38 views
-2

我試圖搶在文本文件中的值(下稱爲#Hostname線)的定義。這用來工作時,主機名純粹是一個整數(int.TryParse),但現在我使用的字符串(string.TryParse)我無法得到它,因爲「‘弦’不包含‘的TryParse’的定義。 「還有什麼我可以使用的?「串」不包含「TryParse'b

private void GetGeneralConfig() 
    { 
     // lets grabs the info from the config! 
     var lines = File.ReadAllLines("general_settings.ini"); 
     var dictionary = lines.Zip(lines.Skip(1), (a, b) => new { Key = a, Value = b }) 
           .Where(l => l.Key.StartsWith("#")) 
           .ToDictionary(l => l.Key, l => l.Value); 
     // lets define variables and convert the string in the dictionary to int for the sock.connection method! 
     string.TryParse(dictionary["#Hostname"], out hostname); 

    } 

回答

8

它已經是一個字符串,所以你並不需要在所有分析它:

hostname = dictionary["#Hostname"];