简体   繁体   中英

DirectoryInfo.GetFiles shows wrong number of files

I am using DirectoryInfo to count the number of files on a network directory:

public static void foo(path)
{
    DirectoryInfo dir = new DirectoryInfo(path);
    dir.Refresh();
    int numOfFiles = dir.GetFiles().Length;
}

I'm encountering a problem that the number of files is incorrect: the method always show the previous number of files since the method was called:

If the directory has 123 files, then numOfFiles = 0 files.

If the directory has 345 files, it shows numOfFiles = 123 files.

If the directory has 678 files, it shows numOfFiles = 345 files, etc.

Any ideas about this? Is it because this method is static ? Is it network location related?

以下代码应按预期工作:

int counts = Directory.GetFiles(path, "*", SearchOption.AllDirectories).Length;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM