0

I have a problem with recursivly scanning through folders and looking for a file. It all work fine, untill some files or folders change. It never updates the folderlistcache it seems.

Is there anyway to refresh or clearcache, so it will rescan the files ?

Thnx in advance!

ArrayList list = new ArrayList();
void dirsearch(string sDir)
{
    try
    {
       foreach (string d in Directory.GetFiles(sDir)) 
       {
         foreach (string f in Directory.GetFiles(d, "*.txt"))
          {
             string foldername = new DirectoryInfo(d).Name;
              String filename = Path.GetFileName("c:\\" + f);
              list.Add("C:\\" + f);
              list.Add(foldername);
              list.Add(filename);
              MessageBox.Show("found one!");
          }
        dirsearch(d);
       }
    }
    catch (System.Exception excpt) 
    {
        MessageBox.Show(excpt.Message);
    }
}
Marice
  • 37
  • 1
  • 5
  • I am very dubious that this code works fine. – Steve Aug 02 '14 at 19:18
  • Matthias' answer is golden. All GetFiles is is a method in the Directory class. You call it once and bam it's done after that. This would have been clear to you if you had noticed the return type for GetFiles being a string array. – Christopher Bruce Aug 02 '14 at 19:36

2 Answers2

1

GetFiles gets the files it finds exactly at the time it scans. There is no Tracing of changes.

It seems like you need the FileSystemWatcher class, which does monitor folders and fires an event, which tells u about file-changes.

For more details, see: http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher%28v=vs.110%29.aspx

Matthias Müller
  • 3,336
  • 3
  • 33
  • 65
0

You can use this overload of GetFiles method, no need for recursion

var files = Directory.GetFiles(sDir, "*.txt", SearchOption.AllDirectories);

edit: fixed typo, and stack overflow requires 6+ character edits.

Community
  • 1
  • 1
Selman Genç
  • 100,147
  • 13
  • 119
  • 184