c#制作搜索框然后在文件夹中显示文件 [英] c# making search box then show files in a folder

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

问题描述

您好,



这是我的搜索条件代码;



Hello,

here is my code for search criteria;

string[] filepaths = Directory.GetFiles(@"c:\data\", textBox1.Text + ".jpg", SearchOption.AllDirectories);





我的表格上有一个文本框。我需要的是当我在文本框中写东西时(比如test),当我点击搜索按钮时,查看c:\ data中的所有test.jpg文件及其windows文件夹中的子目录。



我应该使用openfiledialog吗?我应该如何使用它?



there is a textbox on my form. All i need is when i write something in textbox(like "test"), when i click on search button, see all "test.jpg" files in c:\data and its subdirectories in a windows folder.

should i use openfiledialog? how should i use it?

推荐答案

我应该使用openfiledialog吗?我应该如何使用它?



否 - 字符串数组 filepaths 将包含文件的完整路径集合:OpenFileDialog允许用户选择哪个文件(s他特意想打开。你不能提供一个文件列表供他选择。



你不能在任何标准中显示来自不同文件夹的文件列表控制,或在Windows资源管理器窗口中 - 它们在单个文件夹上工作。

相反,请查看 TreeView控件 [ ^ ]或许 - 它用于显示分层数据,例如文件夹/文件结构,或者或许带有每个图像缩略图的ListView会更好吗?
"should i use openfiledialog? how should i use it?"

No - the array of strings filepaths will contain a collection of full paths to the files: OpenFileDialog allows the user to select which file(s) he specifically wants to open. You can't provide it with a list of files for him to pick from.

You can't display a list of files from different folders in any "standard" control, or in a Windows Explorer window - they work on a single folder.
Instead, look at the TreeView Control[^] perhaps - it is designed for showing hierarchical data such as folder / file structures, or perhaps a ListView with thumbnails of each image would be better?


这取决于很多因素...



如果你愿意的话比如只搜索 C:\ data \ 文件夹,你没有,但如果你想提供更改搜索到的驱动器/文件夹的功能,你需要使用< a href =http://msdn.microsoft.com/en-us/librar y / bb383879%28v = vs.90%29.aspx> FolderBrowserDialog [ ^ ]。
It depends on many factors...

If you would like to search only C:\data\ folder, you don't, but if you would like to provide functionality to change searched drive/folder you need to use FolderBrowserDialog[^].


是的,你可以使用 OpenFileDialog 实现此目的,并将InitialDirectory设置为您首先要搜索的目录,但没有Name特定搜索,只能进行扩展特定搜索,如搜索Office文件或一个网络文件等。



但是,如果我不得不做这个项目。我不会使用 OpenFileDialog ,而是我会使用 System.IO 命名空间并在目录中枚举并寻找我的文件。因为一旦进入这些 System.IO 命名空间的文件目录我可以检查每个文件,按类型,创建时间或名称等。



一旦我抓住了目录中的所有文件,一个简单的这样的名字检查



Yes, you can use the OpenFileDialog to achieve this, and set the InitialDirectory to the directory that you first want to search for, but there isn't a "Name" specific search, only extension specific search can be made, like searching for an Office file or a Web document etc.

However, if I had to do this project. I wouldn't use OpenFileDialog, instead I would have made a use of System.IO namespace and enumerated among the directories and look for my file. Because once inside these System.IO namespace's File or Directory class I can check for each file, by their types, create time or their names etc.

Once I would have caught all of the files inside the directory, a simple name check like this

if(Path.GetFileName(file) == nameToSearch) {
   // match found!
}





文件必须来自目录的文件集。



第二种方法可以给我很大的控制权,但是打开Window explorer中的文件的一件事现在会滞后,因为除非我们使用基于Windows的搜索,否则我们无法按名称过滤文件。但是这个方法可以让我们创建一个简单的UI来显示所有可能的文件,以及在UI中创建的目录供用户导航。



File must come from the directory's file collection.

This second method would have given me much control, but the one thing "open the files in Window explorer" would now lag, since we can't filter the files by their names unless we use a Windows based search. But this method would let us, create a simple UI to show all of the possible files, and directories to create in the UI for the user to navigate.


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

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