开发者

Recursive search method in c#

I decided not to keep the reports in the application as embedded resources anymore, and move them to the server locally. The advantages are obvious, but considering that I want to organize the directories (common reports, letterheads, etc.) in a legible way. I need a way to load reports only by name, calling a recursive search method. My plan is to use a cache that can be built at start of the application and then search in a list OR use a method that seeks report name each time I need... Any ideas and an example eventually with code(c#) are very welcomed!

Folder Structure can be like:

\\webserver\Reports(report files with unique names)

\\webserver\Reports\Common(report files with unique names)

\\webserver\Reports\Manager1(report files with unique names)

\\webserver\Reports\Manager1\Invoices(report files开发者_开发知识库 with unique names)

\\webserver\Reports\ManagerN(report files with unique names)

Hope to be useful this exemplification! Thank you!


If you want to search directories recursivly for a certain extension:

var d = new DirectoryInfo("\\\\webserver\\Reports");
var files =  d.GetFiles(".rpt", SearchOption.AllDirectories);


How about instead of searching every time, you record the exact location of the reports in a section of your application's configuration file? Maybe that is what you mean by building a cache.


please refer to this article by Microsoft How to recursively search directories by using Visual C#

It explains how to write your recursive function (which is very simple in c#)

If your directory tree is not too huge (less than 100 dirs and less than, say, 50 files in each dir), then a cache is not necessary (in my humble opinion). If you build a cache, you have to take care of it (ie. handle the case when it has to be updated etc)

void DirSearch(string sDir) {
try 
{
   foreach (string d in Directory.GetDirectories(sDir)) 
   {
    foreach (string f in Directory.GetFiles(d, txtFile.Text)) 
    {
       lstFilesFound.Items.Add(f);
    }
    DirSearch(d); /// Recursive Call !!
   }
}
catch (System.Exception excpt) 
{
    Console.WriteLine(excpt.Message);
}}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜