从目录问题中读取多个文件 [英] read multiple files from a directory problem
问题描述
嗨大家好
我正在尝试从目录中读取多个文件,但问题在于,我得到多次相同的输出重复。
ListBox仅用于展示目的
请参阅下面的代码
< pre lang =c#> protected void GetFileValues()
{
< span class =code-keyword> var fileEntries = Directory.GetFiles(Server.MapPath( DataFiles ), * .whl);
List< string> list = new List< string>();
foreach ( string files in fileEntries)
{
string lineRead;
StreamReader reader = new StreamReader(files);
while ((lineRead = reader.ReadLine())!= null )
{
list.Add(lineRead);
} // 结束时
reader.Close();
ListBox1.Items.Add(list [ 5 ]。ToString());
}
}
当前输出--------------- -----正确输出
John ------------------------------> John
John ------------------------------> Aubrey
John ------------------------------> Thomas
John ------------------------------> Neil
ListBox1.Items.Add(list [5] .ToString());
你在循环中调用它。因此,您将重复添加到列表框中的相同内容。
Hi Guys
I'm trying to read multiple files from a directory but the issue here is that, I get same output repeating multiple times.
The ListBox is just for display purposes
please see my code below
protected void GetFileValues()
{
var fileEntries = Directory.GetFiles(Server.MapPath("DataFiles"), "*.whl");
List<string> list = new List<string>();
foreach(string files in fileEntries)
{
string lineRead;
StreamReader reader = new StreamReader(files);
while ((lineRead = reader.ReadLine()) != null)
{
list.Add(lineRead);
}//end while
reader.Close();
ListBox1.Items.Add(list[5].ToString());
}
}
current output--------------------Correct output
John------------------------------> John
John------------------------------> Aubrey
John------------------------------> Thomas
John------------------------------> Neil
ListBox1.Items.Add(list[5].ToString());
You are calling this in a loop. Therefore you will get the same thing added to the listbox repeatedly.
这篇关于从目录问题中读取多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!