2012-05-31 24 views
5

我有這樣的代碼:有沒有辦法從網站上讀取,每次只讀一行?

string downloadedString; 
System.Net.WebClient client; 

client = new System.Net.WebClient(); 

downloadedString = client.DownloadString(
    "http://thebnet.x10.mx/HWID/BaseHWID/AlloweHwids.txt"); 

這是一個HWID型安全性(它會檢查你的HWID,看看是否被允許使用的程序)

無論如何,我希望能夠把例如:

xjh94jsl <-- Not a real HWID 
t92jfgds <-- Also not real 

並且能夠逐一讀取每一行,並將其更新到downloadString。

回答

18

不要將URL作爲字符串下載,請將其作爲流讀取。

using System.IO; 
using System.Net; 

var url ="http://thebnet.x10.mx/HWID/BaseHWID/AlloweHwids.txt"; 
var client = new WebClient(); 
using (var stream = client.OpenRead(url)) 
using (var reader = new StreamReader(stream)) 
{ 
    string line; 
    while ((line = reader.ReadLine()) != null) 
    { 
     // do stuff 
    } 
} 
+0

我需要一個參考來使用它嗎? – Minicl55

+0

參考什麼? –

+0

我不知道。它不斷給我一個錯誤,它缺少使用StreamReader和WebClient的程序集引用。 – Minicl55

相關問題