搜索目录C#中的文件 [英] search files in directory C#

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

问题描述

我需要在一个目录中找到一个文件,里面有很多文件夹。



我写了一个代码来执行它,因为目录有更多其中包含1000个文件夹,获取结果需要更多时间。



我的代码:



I need to find a file within a directory which has lot many folder within it.

I have written a code which does it, since the directory has more than 1000 folders in it, it takes more time to fetch the result.

My code :

protected void Button1_Click(object sender, System.EventArgs e)
   {
       string search = TextBox1.Text;
       string[] files = Directory.GetFiles(@"\\directory1\storage\folder1\", "*.*", SearchOption.AllDirectories);
       int Flag = 0;
       string dir = @"\\directory1\storage\folder1\";
       string[] files1;
       int numFiles;
       files1 = Directory.GetFiles(dir);
       numFiles = files.Length;
       Response.Write("Files searched : " + numFiles + "<br>");
       for (int i = 0; i < numFiles; i++)
       {


           string file = files[i].ToString();

           int file1 = file.IndexOf(search,StringComparison.CurrentCultureIgnoreCase);
           if (file1 != -1)
           {
               Response.Write("<br>" + file);
               //Response.Write("<div style='position:absolute;top:" + file + "px;left:" +  "px'>"+file! + "</div>");
               Flag = 1;

           }




       }
       if (Flag == 0)
       {
           Response.Write("No Matches Found");
       }
   }







请建议我,可能会发布代码。




Kindly suggest me, possibly post the code.

推荐答案

方法 Directory.GetFiles 有一个令人讨厌的警告,但不是很清楚。



请参阅此讨论(以及我的回答);你会找到确切的固定解决方案: Directory.Get.Files搜索模式问题 [ ^ ]。



-SA
The method Directory.GetFiles has one nasty caveat which is not very well know.

Please see this discussion (and my answer, too); you will find exact fixed solution: Directory.Get.Files search pattern problem[^].

—SA


我认为你可以提供搜索字符串Directory.GetFiles本身。喜欢(Myfile * .txt或MyFile.txt)。不知道这是否会提高速度。试试吧。:)



jkchan

http://cgmath.blogspot.com
I think you can provide search string in Directory.GetFiles itself. like ( Myfile*.txt or MyFile.txt ). Don't know whether this will improve the speed. Just try .:)

jkchan
http://cgmath.blogspot.com


根据您的要求,我知道您要搜索文件夹中的特定文件,包括子文件夹。



你可以这么简单..

As per your requirement, i understood you want to search a specific file in the folder including sub folders.

you can you simple like this..
string[] files = Directory.GetFiles("c:\\", "*.exe", SearchOption.AllDirectories);





希望这可以帮到你



--RA



Hope this may helps you

--RA


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

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