2012-02-24 49 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); 
}