Search code examples
c#cachingdirectoryrefreshgetfiles

Directory.GetFiles does not refresh the results/cache


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);
    }
}

Solution

  • 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.