且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

搜索文件夹中的文件

更新时间:2023-01-15 18:58:11


请仔细阅读以下内容-
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.