2012-02-24 55 views
7

我有讀文件,StreamReader同時line != null添加到一個問題textBox1C#從文件中讀取符合的StreamReader與DownloadFileAsync

代碼:

using(StreamReader reader = new StreamReader("lastupdate.txt")) 
{ 
    string line; 

    while((line = reader.ReadLine()) != null) 
    { 
     textBox1.Text = line; 
    } 

    reader.Close(); 
} 

它不工作,我不知道爲什麼。我試圖使用using StreamReader,我從URL下載文件,我可以在文件夾中看到該文件已下載。 lastupdate.txt大小爲1KB。

這是我目前使用的代碼MessageBox。如果我刪除MessageBox,代碼不起作用。它需要某種等待或我不知道:

WebClient client = new WebClient(); 

client.DownloadFileAsync(new Uri(Settings.Default.patchCheck), "lastupdate.txt"); // ok 

if(File.Exists("lastupdate.txt")) 
{ 
    MessageBox.Show("Lastupdate.txt exist"); 
    using(StreamReader reader = new StreamReader("lastupdate.txt")) 
    { 
     string line; 

     while((line = reader.ReadLine()) != null) 
     { 
      textBox1.Text = line; 
      MessageBox.Show(line.ToString()); 
     } 

     reader.Close(); 
    } 

    File.Delete("lastupdate.txt"); 
} 
+1

textBox1.Text = text? textBox1.Text + =行? – shenhengbin 2012-02-24 14:13:26

+0

你確定,雖然被執行,讀者有價值嗎? – Akrem 2012-02-24 15:47:10

+0

你是什麼意思? lastupdate.txt包含數據「1」只是數字... – user1085907 2012-02-24 16:15:18

回答

13

嘗試:

StringBuilder sb = new StringBuilder(); 
using (StreamReader sr = new StreamReader("lastupdate.txt")) 
{ 
    while (sr.Peek() >= 0) 
    { 
     sb.Append(sr.ReadLine()); 
    } 
} 
textbox.Text = sb.Tostring(); 
8

如果你想在文本框中的文本,將更加有效地閱讀這一切然後把它在文本框中:

var lines = File.ReadAllLines("lastupdate.txt"); 
textBox1.Lines = lines; //assuming multi-line text box 

或:

textBox1.Text = File.ReadAllText("lastupdate.txt"); 

編輯:

最新更新後 - 你正在下載文件異步 - 它甚至可能不存在,只是部分存在或狀態代碼執行時,在兩者之間。

如果你只是想要的文件沒有下載的文本字符串,使用DownloadString代替:

string text = ""; 
using (WebClient wc = new WebClient()) 
{ 
    text = wc.DownloadString(new Uri(Settings.Default.patchCheck)); 
} 
textBox1.Text = text; 
+1

如果文件真的很大,該怎麼辦? – wal 2012-02-24 14:14:28

+1

然後原來的版本更糟糕,因爲UI會被更新請求淹沒(即使讀取是在後臺線程上完成的)。如果它真的很大,閱讀文本應該在後臺線程上完成。 – BrokenGlass 2012-02-24 14:16:25

+4

@wal:如果文件很大,那麼問題與文本框包含所有行和問題的想法是錯誤的 – Akrem 2012-02-24 14:19:52

0

上面給出的答案是正確的,但在你的代碼,只需更改1線:

textBox1.Text += line; 
+0

不工作:( – user1085907 2012-02-24 14:44:27

+0

什麼是不工作?你確定,你有'+ ='? – mindandmedia 2012-02-24 18:04:23

3

試試這個:

using(StreamReader reader = new StreamReader(Path)) 
{ 
    string line = reader.ReadLine(); 

    while(line != null) 
    { 
     textBox1.Text += line; 
     line = reader.ReadLine() 
    } 

    reader.Close(); 
} 
+0

也不工作:'( – user1085907 2012-02-24 14:48:31

1

Web客戶端有一個相當奇特的DownloadFileAsy nc方法。返回類型是無效的,所以不能等待。此外,這意味着我們甚至沒有得到一個任務,所以ContinueWith是不可能的。這使我們使用DownloadFileCompleted事件。

const string FileName = "lastupdate.txt"; 

private void DownloadLastUpdate() { 
    var client = new WebClient(); 

    client.DownloadFileCompleted += (s, e) => { 
     this.UpdateTextBox(e.Error); 
     client.Dispose(); 
    }; 

    client.DownloadFileAsync(new Uri(Settings.Default.patchCheck), FileName); 
} 

我去了一個可選的異常參數來中繼任何異常消息。隨意根據需要重構。 File.ReadLines逐行生成文本,因此大文件不應占用太多內存。

private void UpdateTextBox(Exception exception = null) { 
    textBox1.Text = string.Empty; 

    if (exception != null) { 
     textBox1.Text = exception.Message; 
     return; 
    } 

    if (!File.Exists(FileName)) { 
     textBox1.Text = string.Format("File '{0}' does not exist.", FileName); 
     return; 
    } 

    var lines = File.ReadLines(FileName); 

    textBox1.Text = string.Join(Environment.NewLine, lines); 
}