C# 使用 StreamReader 和 DownloadFileAsync 从文件中读取行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9432108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# read line from file with StreamReader with DownloadFileAsync
提问by user1085907
I am having a problem reading file with StreamReaderand while line != nulladd to textBox1
我在读取文件时遇到问题StreamReader,同时line != null添加到textBox1
Code:
代码:
using(StreamReader reader = new StreamReader("lastupdate.txt"))
{
string line;
while((line = reader.ReadLine()) != null)
{
textBox1.Text = line;
}
reader.Close();
}
It's not working and I don't know why. I tried to use using StreamReader, I download the file from the URL and I can see in the folder that the file is downloaded. The lastupdate.txtis 1KB in size.
它不起作用,我不知道为什么。我尝试使用using StreamReader,我从 URL 下载文件,我可以在下载文件的文件夹中看到。该lastupdate.txt是大小1KB。
This is my current working code with MessageBox. If I remove the MessageBox, the code doesn't work. It needs some kind of wait or I don't know:
这是我当前使用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");
}
回答by Pranay Rana
Try :
尝试 :
StringBuilder sb = new StringBuilder();
using (StreamReader sr = new StreamReader("lastupdate.txt"))
{
while (sr.Peek() >= 0)
{
sb.Append(sr.ReadLine());
}
}
textbox.Text = sb.Tostring();
回答by BrokenGlass
If you want the text in the text box it would be much more effective to read all of itand then put it into the text box:
如果您想要文本框中的文本,阅读所有内容然后将其放入文本框中会更有效:
var lines = File.ReadAllLines("lastupdate.txt");
textBox1.Lines = lines; //assuming multi-line text box
or:
或者:
textBox1.Text = File.ReadAllText("lastupdate.txt");
Edit:
编辑:
After latest update - you are downloading the file asynchronously- it might not even be there, only partially there or in a state in-between when your code executes.
在最新更新之后 - 您正在异步下载文件- 它甚至可能不存在,仅部分存在或在您的代码执行时处于中间状态。
If you just want the text string in the file don't download it, use DownloadStringinstead:
如果您只想不下载文件中的文本字符串,请DownloadString改用:
string text = "";
using (WebClient wc = new WebClient())
{
text = wc.DownloadString(new Uri(Settings.Default.patchCheck));
}
textBox1.Text = text;
回答by mindandmedia
the answer given above is correct, but in your piece of code, just change 1 line:
上面给出的答案是正确的,但在您的代码中,只需更改 1 行:
textBox1.Text += line;
回答by Akrem
Try this :
尝试这个 :
using(StreamReader reader = new StreamReader(Path))
{
string line = reader.ReadLine();
while(line != null)
{
textBox1.Text += line;
line = reader.ReadLine()
}
reader.Close();
}
回答by thestud2012
Web Client has a rather bizarre DownloadFileAsync method. The return type is void, so it is not awaitable. Also, that means we do not even get a Task, so ContinueWith is not possible. That leaves us with using the DownloadFileCompleted event.
Web Client 有一个相当奇怪的 DownloadFileAsync 方法。返回类型为 void,因此不可等待。此外,这意味着我们甚至没有得到 Task,所以 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 );
}
I went with an optional exception parameter to relay any exception messages. Feel free to refactor as desired. File.ReadLines yields text line by line, so large files should not use very much memory.
我使用了一个可选的异常参数来中继任何异常消息。随意重构。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 );
}

