2015-08-20 46 views
-3

我目前正在開發的C#應用​​程序,我需要從一個txt文件中恢復襪子的值這裏是txt文件獲得價值C#

104.131.163.123:2541信息
104.131.178.167:2541

我需要行的每一行讀取文件中的行和檢索的IP地址和端口值的價值,並把它們放在一個列表

這是我的代碼:

我需要行的每一行讀取文件中的行和檢索的IP地址和端口值的價值,並把它們放在一個列表 這是我的代碼

List<string[]> list = new List<string[]>(); 
      StreamReader sr = new StreamReader (@"C:\"); 
      string line; 
      while ((line = sr.ReadLine()) != null)`enter code here` 
      { 
       string[] array = line.Spit(":"); 
       list.Add(array); 
      } 

謝謝

+1

Hwat您嘗試過目前爲止嗎?你卡在哪裏?有很多關於閱讀文本文件的教程(包括[MSDN文檔](https://msdn.microsoft.com/en-us/library/db5x7c0d.aspx)) –

+0

我在多本書中搜索MSDN,但沒有結果 – user3537566

+1

您是否首先搜索[如何從txt文件讀取](https://www.google.com/search?q=+how+to+read+from+txt+file+c%23&ie=utf-8&oe=utf -8)? –

回答

1

以下代碼讀取文件的所有行並將每個ip地址添加到列表中

private List<string> GetIPAddress() 
{ 
    var list = new List<string>(); 
    var input = File.ReadAllText("file.txt"); 
    var r = new Regex(@"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{1,5})"); 
    foreach (Match match in r.Matches(input)) 
    { 
     string ip = match.Groups[1].Value; 
     string port = match.Groups[2].Value; 
     list.Add(ip); 
     // you can also add port in the list 
    } 
    return list; 
} 
+0

thk的工作 – user3537566

+0

@ user3537566歡迎:) –

+0

我想改變這一切在函數返回給我的IP和端口列表我應該做什麼? – user3537566