如何刷新 System.IO.DirectoryInfo.GetFiles().Length

发布于 2024-10-07 10:26:37 字数 449 浏览 0 评论 0原文

我在获取目录中的最新文件数量时遇到问题。文件正在从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一场春暖 2024-10-14 10:26:37

count 是本地 int - 更新它的唯一方法是再次查询它。尝试将 pdf.Refresh() 替换为:(

count = pdf.GetFiles().Length;

实际上,Directory.GetFiles(di.FullName).Length 可能更便宜)

但是!您不想想要在紧密的循环中执行此操作;也许添加一个Sleep(1000),或者(更好)使用FileSystemWatcher。更好的是;检查特定文件,这样您就不会主动点击GetFiles()

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文