How to refresh System.IO.DirectoryInfo.GetFiles().Length
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.Directo开发者_开发百科ryInfo(@"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.
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.
精彩评论