搜索文件夹中的文件 [英] Search files in a folder

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

问题描述


我正在开发一个应用程序,该应用程序已将seach文件包含在给定的文件夹中,例如在 Windows 7开始菜单中..在该应用程序中,我想在Textbox_TextChanged事件中进行搜索,但我不知道我是否需要触发任何查询.请告诉我该怎么办?

我必须在DataGridView和任何带有扩展名的文件类型中显示结果.结果应在Textbox_TextChanged

Hi,
I am developing an application for which i have included seach files in given folder one like in Windows 7 start menu. in which i want to search it on Textbox_TextChanged Event but i dont know whether i need to fire any query on anything. Please tell me what should i do?

I have to show result in DataGridView and any file type with extension. result should change on Textbox_TextChanged

推荐答案


请仔细阅读以下内容-
http://msdn.microsoft.com/en-us/library/bb266451 (v = VS.85).aspx [
Hi,
Go through the following-
http://msdn.microsoft.com/en-us/library/bb266451(v=VS.85).aspx[^]


在Visual Studio设计器中,突出显示您的TextBox.
查看属性"窗格.
单击看起来像闪电的按钮-该按钮将向您显示事件而不是属性.
向下滚动到TextChanged.双击它.
这将为您连接一个事件处理程序,并带您进入用户更改文本框中的文本时调用的方法.

现在,您可以在方法中编写代码-当用户在TextBox中键入内容时,将为每个字符调用该方法.
In the Visual Studio designer, highlight your TextBox.
Look at the Properties pane.
Click the button that looks like a lightning bolt - that will show you the Events instead of the Properties.
Scroll down to TextChanged. Double click it.
That will hook up an event handler for you, and take you to the method that is called when the user changes the text in the text box.

You can now write your code in the method - when the user types in the TextBox, that method will be called for each character.


首先要做的是将其添加到using部分如果还不存在:
The first thing to do is add this to the using section if not there already:
using System.IO;
using System.Data;



要从文件夹中获取文件,可以使用:



To get the files from a folder, you can use:

string[] foundFiles = Directory.GetFiles(thePathVariable, theSearchBoxText + "*.*");



您必须更改"thePathVariable"和"theSearchBoxText"以适应您的需求.
通过添加通配符字符串("*.*"),您将搜索每个文件扩展名都以搜索框中写入的文本开头的文件.您可以使用通配符来满足其他任何需求.

为了将结果显示到datagridview中,我们必须将数据从字符串数组传输到数据表.使用LINQ也可以轻松实现这一点,但是您似乎还不很了解,因此:



You have to change "thePathVariable" and "theSearchBoxText" to suite your needs.
By appending the wildcard string ("*.*") you''re searching for every file with any extension that starts with the text written in your search box. You can play with the wildcards to achieve any other needs.

To display the results into a datagridview, we have to transfer the data from the string array to a datatable. This can be also achieved easily with LINQ, but seems you''re not quite there, so:

DataTable s = new DataTable();
s.Columns.Add("FileName");

foreach (string aFile in foundFiles)
{
   s.Rows.Add(aFile);
}
dataGridView1.DataSource = s;



就是这样,希望对您有所帮助.



And that''s it, hope it helps.


这篇关于搜索文件夹中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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