如何刷新 System.IO.DirectoryInfo.GetFiles().Length
我在获取目录中的最新文件数量时遇到问题。文件正在从 PDFCreator 打印并发送到该文件夹。当文件夹中的文件数量与正在打印的文件数量匹配时,它应该中断并继续我的代码。问题是计数没有保持最新,我不知道如何刷新它。这是我的代码:
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;
}
}
如果有人可以告诉我如何刷新或更新文件计数,我将非常感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
count
是本地int
- 更新它的唯一方法是再次查询它。尝试将pdf.Refresh()
替换为:(实际上,
Directory.GetFiles(di.FullName).Length
可能更便宜)但是!您不想想要在紧密的循环中执行此操作;也许添加一个
Sleep(1000)
,或者(更好)使用FileSystemWatcher
。更好的是;检查特定文件,这样您就不会主动点击GetFiles()
。count
is a localint
- the only way to update that would be to query it again. Try replacingpdf.Refresh()
with:(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) useFileSystemWatcher
. Even better still; check for a specific file so you don't hitGetFiles()
aggressively.