2

I'm having trouble with getting the uptodate amount of files in a directory. The files are being printed from PDFCreator and being sent to that folder. When the number of files in the folder match the number of files being printed it should then break and continue with my code. The problem is the count doesn't keep uptodate and I do not know how to refresh it. This is my code:

System.IO.DirectoryInfo pdf = new System.IO.DirectoryInfo(@"C:\0TeklaBatchProcess\pdf");
int count = pdf.GetFiles().Length;

while (count != DE.GetSize())
{
    if (count < DE.GetSize())
    {
        pdf.Refresh();
    }
    else
    {
        break;
    }
}

If someone can tell me how to refresh or update the count of files I'd appreciate it a lot.

Mutley
  • 77
  • 2
  • 13

1 Answers1

5

count is a local int - the only way to update that would be to query it again. Try replacing pdf.Refresh() with:

count = pdf.GetFiles().Length;

(actually, Directory.GetFiles(di.FullName).Length is probably cheaper)

However! You don't want to do this in a tight loop; maybe add a Sleep(1000), or (better) use FileSystemWatcher. Even better still; check for a specific file so you don't hit GetFiles() aggressively.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • count = pdf.GetFiles().Length worked. Thanks so much for that, can't believe I didn't try it. I had tried Sleep but it didn't work because the count still hadn't updated. I can't check for a specific file because the names of the files are different all the time. Even though it's working now I'm interested in how to use FileSystemWatcher to check the amount of files since you said it was better. Can you show me how? – Mutley Dec 09 '10 at 20:16
  • @Mutley - sorry, that isn't something I've done except for a few test projects – Marc Gravell Dec 09 '10 at 20:45
  • ok, no problem. Thanks again! I'd vote up on your answer but don't have enough rep.. – Mutley Dec 09 '10 at 20:59