循环通过FileUpload控件上传的txt文件行 [英] Looping through lines of txt file uploaded via FileUpload control
本文介绍了循环通过FileUpload控件上传的txt文件行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用FileUpload控件选择一个包含字符串行的简单.txt文件.但是,与其实际保存文件,不如我要遍历文本的每一行并在ListBox控件中显示每一行.
I want to select a simple .txt file that contains lines of strings using a FileUpload control. But instead of actually saving the file I want to loop through each line of text and display each line in a ListBox control.
文本文件示例:
test.txt
123jhg345
182bdh774
473ypo433
129iiu454
123jhg345
182bdh774
473ypo433
129iiu454
完成此任务的最佳方法是什么?
What is the best way to accomplish this?
到目前为止我所拥有的:
What I have so far:
private void populateListBox()
{
FileUpload fu = FileUpload1;
if (fu.HasFile)
{
//Loop trough txt file and add lines to ListBox1
}
}
推荐答案
private void populateListBox()
{
FileUpload fu = FileUpload1;
if (fu.HasFile)
{
StreamReader reader = new StreamReader(fu.FileContent);
do
{
string textLine = reader.ReadLine();
// do your coding
//Loop trough txt file and add lines to ListBox1
} while (reader.Peek() != -1);
reader.Close();
}
}
这篇关于循环通过FileUpload控件上传的txt文件行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文