0

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!

2
  • 1
    What reporting product are you using? Is there any reason you can't have a lookup table populated from config file entries, i.e Report ABC is located at /webserver/reports/repoart ABC.aspx?
    – slugster
    Jan 25, 2010 at 9:51
  • I use Crystal reports for a desktop application with unknown number of reports and/or paths(but unique file names in that directory structure "\\webserver\reports").
    – andySF
    Jan 25, 2010 at 11:53

3 Answers 3

5

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

var d = new DirectoryInfo("\\\\webserver\\Reports");
var files =  d.GetFiles(".rpt", SearchOption.AllDirectories);
1
  • One question. What is the best option? To fill a Dictionary(FileName,Path) or to fill an FileInfo[] array for future findings? it's a difference in speed? I like FileInfo array for his complete information about file. i don't need it for now but in future may be useful! Long story short,a dictionary search or an array search? What is your opinion? Thank you!
    – andySF
    Jan 25, 2010 at 12:00
1

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.

3
  • yes, that is what i mean to say. i need an advice about better practices in this situations. You think that storing all files at application start up is good. I think storing paths and names in a dictionary or data table at start up is the best solution. Then i can look up for them there and eventually if is not found i can make a second search just in case. Thank you so much for you quick response!
    – andySF
    Jan 25, 2010 at 10:09
  • I was suggesting that if you have a finite number of reports and your application is referencing them, you must know the paths to the files and can record those in the configuration file. You can create your in-memory directory from that, instead of going through the filesystem. Then, if an expected report is found not to be there, you can try a search or otherwise handle it.
    – Jay
    Jan 25, 2010 at 10:25
  • oh, i see. it's not a finite number of reports and the names(report.rpt) are stored in a sql data table(instead of a dictionary in configuration file). the sql table i found more accessible in editing and managing reports. Thank you!
    – andySF
    Jan 25, 2010 at 10:31
1

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);
}}
1
  • I don't have so much files. Max 20,30 reports per directory(max 20 dirs) that can grow but never to reach like 100 dirs :). That method i already read and customized for my use. Thank you!
    – andySF
    Jan 25, 2010 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.