Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何刷新System.IO.DirectoryInfo.GetFiles().Length_C#_Directoryinfo_Getfiles - Fatal编程技术网

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

C# 如何刷新System.IO.DirectoryInfo.GetFiles().Length,c#,directoryinfo,getfiles,C#,Directoryinfo,Getfiles,我在获取目录中最新数量的文件时遇到问题。文件正在从PDFCreator打印并发送到该文件夹。当文件夹中的文件数与正在打印的文件数匹配时,它将中断并继续使用我的代码。问题是计数不能保持最新,我不知道如何刷新它。这是我的代码: System.IO.DirectoryInfo pdf = new System.IO.DirectoryInfo(@"C:\0TeklaBatchProcess\pdf"); int count = pdf.GetFiles().Length; while (count

我在获取目录中最新数量的文件时遇到问题。文件正在从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;
    }
}
System.IO.DirectoryInfo pdf=new System.IO.DirectoryInfo(@“C:\0TeklaBatchProcess\pdf”);
int count=pdf.GetFiles().Length;
while(count!=DE.GetSize())
{
如果(计数

如果有人能告诉我如何刷新或更新文件计数,我将不胜感激。

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

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


然而!你不想在一个紧密的循环中做这件事;可能会添加一个
睡眠(1000)
,或者(更好)使用
文件系统监视程序。甚至更好;检查一个特定的文件,这样你就不会攻击性地点击
GetFiles()

count=pdf.GetFiles()。长度有效。非常感谢,真不敢相信我没试过。我试过睡觉,但没用,因为计数仍然没有更新。我无法检查特定文件,因为文件的名称始终不同。尽管它现在可以工作了,但我对如何使用FileSystemWatcher检查文件量感兴趣,因为您说它更好。你能告诉我怎么做吗?@Mutley-对不起,除了一些测试项目,我没做过什么。好的,没问题。再次感谢!我会对你的答案投赞成票,但没有足够的支持率。。