C#使用StreamReader和DownloadFileAsync从文件中读取行
当C#使用StreamReader和DownloadFileAsync从文件中读取行,c#,streamreader,readline,webclient-download,C#,Streamreader,Readline,Webclient Download,当StreamReader和line!=空添加到文本框1 代码: 它不起作用,我不知道为什么。我尝试使用StreamReader使用,我从URL下载了文件,我可以在文件夹中看到文件已下载。lastupdate.txt的大小为1KB 这是我当前使用MessageBox的工作代码。如果我删除消息框,则代码不起作用。需要等待,否则我不知道: WebClient client = new WebClient(); client.DownloadFileAsync(new Uri(Settings.De
StreamReader
和line!=空
添加到文本框1
代码:
它不起作用,我不知道为什么。我尝试使用StreamReader使用,我从URL下载了文件,我可以在文件夹中看到文件已下载。lastupdate.txt
的大小为1KB
这是我当前使用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");
}
尝试:
StringBuilder sb = new StringBuilder();
using (StreamReader sr = new StreamReader("lastupdate.txt"))
{
while (sr.Peek() >= 0)
{
sb.Append(sr.ReadLine());
}
}
textbox.Text = sb.Tostring();
如果您想在文本框中输入文本,则阅读所有文本,然后将其放入文本框中会更加有效:
var lines = File.ReadAllLines("lastupdate.txt");
textBox1.Lines = lines; //assuming multi-line text box
或:
编辑:
在最新更新之后-您正在异步下载文件-它甚至可能不在那里,只是部分在那里,或者在代码执行时处于介于两者之间的状态
如果您只是想要文件中的文本字符串,而不是下载它,请改用DownloadString
:
string text = "";
using (WebClient wc = new WebClient())
{
text = wc.DownloadString(new Uri(Settings.Default.patchCheck));
}
textBox1.Text = text;
上面给出的答案是正确的,但在您的代码中,只需更改1行:
textBox1.Text += line;
试试这个:
using(StreamReader reader = new StreamReader(Path))
{
string line = reader.ReadLine();
while(line != null)
{
textBox1.Text += line;
line = reader.ReadLine()
}
reader.Close();
}
Web客户端有一个相当奇怪的DownloadFileAsync方法。返回类型为void,因此不可等待。而且,这意味着我们甚至没有任务,所以继续下去是不可能的。这让我们只能使用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 );
}
我使用了一个可选的异常参数来传递任何异常消息。可以根据需要随意重构。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 );
}
textBox1.Text=文本?Text box1.Text+=line?你确定while被执行,读卡器有值吗?你是什么意思?lastupdate.txt只包含数据“1”和数字…由于您正在下载Async
,因此基本上将跳过其余代码if(File.Exists)
将为false,因为该文件尚未存在或下载线程正在使用。这就是为什么你的文本框里什么都没有。您需要设置一个事件处理程序来处理Ansyc请求。当您使用MessageBox暂停代码的执行时,您允许文件完全下载。可以在(!reader.EndOfStream)
期间尝试,然后分配ReadLine()
值?如果文件真的很大怎么办?那么原始版本更糟糕,因为UI将被更新请求淹没(即使读取是在后台线程上完成的)。如果文件太大,阅读文本也应该在后台线程上进行。@wal:如果文件太大,那么文本框包含所有行和问题的想法都是错误的。这很好,你同意Akrem@Ravia的说法,那就增加他的评论,而不是发布你自己无用的评论。我认为值得一提ng在回答中作为免责声明,即“如果你的文件不大,那么这个解决方案很好”,如果文件真的很大呢?不是1kb吗
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 );
}
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 );
}