[repost]如何使用C#列出列表框中搜索文件夹的内容(文件)? [英] [repost] How to list the contents(files) of a search folder in listbox, using C#?

查看:87
本文介绍了[repost]如何使用C#列出列表框中搜索文件夹的内容(文件)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



这里我有一个问题,就是如何在列表框中列出搜索文件夹的内容(文件)。问题是我有很多文件夹,每个文件夹都有许多格式为.txt的文件。我需要,如果我在文本框中键入文件夹名称并单击按钮,则在列表框中显示搜索文件夹中的所有文件。在下面我添加我的代码,但它不起作用。请帮帮我....



<前lang =cs> 受保护 void Button1_Click( object sender,EventArgs e)
{
ListBox1.Items.Clear();
string search = TextBox1.Text; // 此处输入文件夹名称
if (搜索!=
// DirectoryInfo d = new DirectoryInfo(@\\192.123.1.18\Report\Result+ search);
{
string [] files = Directory.GetFiles( @ \\192.123.1.16\Report\Result\ + search + * + *。*);
foreach 字符串文件 in 文件)
{
// ListBox1.Items.Add(new ListItem(Path.GetFileName) (文件),文件));
ListBox1.Items.Add( new ListItem(Path.GetFileName(file),file)); // 列出搜索文件夹中的所有文件
}
{
search = ;
}
}

else
{
Response.Write( < script> alert('请输入搜索关键字');< / script>);
}
}

解决方案

嗨朋友们,我准备好了解决问题。我在代码中犯了一些错误。顺便感谢朋友的帮助。修改后的代码是:



 string [] files = Directory.GetFiles(@\\192.123.1.16\Report \结果\+搜索,* .txt,SearchOption.AllDirectories); 





问候,

Thanes


Hi friends,

Here i have a problem, which is how to list the contents(files) of a search folder in listbox. The issue is i have many folder, each folder have many files with format .txt. I need, if i type the folder name in textbox and click the button, in the listbox shows all the files inside the search folder. In below i add my code, but it not works. Please help me....

protected void Button1_Click(object sender, EventArgs e)
       {
           ListBox1.Items.Clear();
           string search = TextBox1.Text; // here type the folder name
           if (search != "")
           //DirectoryInfo d = new DirectoryInfo(@"\\192.123.1.18\Report\Result" + search);
           {
               string[] files = Directory.GetFiles(@"\\192.123.1.16\Report\Result\"+ search + "*" + "*.*");
               foreach (string file in files)
               {
                   //ListBox1.Items.Add(new ListItem(Path.GetFileName(file), file));
                   ListBox1.Items.Add(new ListItem(Path.GetFileName(file), file)); // listed all files in the search folder
               }
               {
                   search = "";
               }
           }

           else
           {
               Response.Write("<script>alert('Please Enter Search Keyword');</script>");
           }
       }

解决方案

Hi Friends, i m settle ready the problem. i do some mistake in code. By the way thanks friends for the help. The modified code is:

string[] files = Directory.GetFiles(@"\\192.123.1.16\Report\Result\"+ search, "*.txt", SearchOption.AllDirectories);



Regards,
Thanes


这篇关于[repost]如何使用C#列出列表框中搜索文件夹的内容(文件)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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