从目录问题中读取多个文件 [英] read multiple files from a directory problem

查看:87
本文介绍了从目录问题中读取多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨大家好



我正在尝试从目录中读取多个文件,但问题在于,我得到多次相同的输出重复。



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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆