gpt4 book ai didi

c# - 快速获取特定路径下的所有文件和目录

转载 作者:IT王子 更新时间:2023-10-29 03:42:53 27 4
gpt4 key购买 nike

我正在创建一个备份应用程序,其中 C# 扫描目录。在我使用这样的东西来获取目录中的所有文件和子文件之前:

DirectoryInfo di = new DirectoryInfo("A:\\");
var directories= di.GetFiles("*", SearchOption.AllDirectories);

foreach (FileInfo d in directories)
{
//Add files to a list so that later they can be compared to see if each file
// needs to be copid or not
}

唯一的问题是有时无法访问文件并且出现多个错误。我得到的错误示例是:error

因此,我创建了一个递归方法,它将扫描当前目录中的所有文件。如果该目录中有 where 目录,则将通过该目录再次调用该方法。这种方法的好处是我可以将文件放在 try catch block 中,这样我就可以选择在没有错误的情况下将这些文件添加到列表中,并在出现错误时将目录添加到另一个列表中。

try
{
files = di.GetFiles(searchPattern, SearchOption.TopDirectoryOnly);
}
catch
{
//info of this folder was not able to get
lstFilesErrors.Add(sDir(di));
return;
}

所以这个方法很有效,唯一的问题是当我扫描一个大目录时,它需要很多次。我怎样才能加快这个过程?我的实际方法是这样的,以备不时之需。

private void startScan(DirectoryInfo di)
{
//lstFilesErrors is a list of MyFile objects
// I created that class because I wanted to store more specific information
// about a file such as its comparePath name and other properties that I need
// in order to compare it with another list

// lstFiles is a list of MyFile objects that store all the files
// that are contained in path that I want to scan

FileInfo[] files = null;
DirectoryInfo[] directories = null;
string searchPattern = "*.*";

try
{
files = di.GetFiles(searchPattern, SearchOption.TopDirectoryOnly);
}
catch
{
//info of this folder was not able to get
lstFilesErrors.Add(sDir(di));
return;
}

// if there are files in the directory then add those files to the list
if (files != null)
{
foreach (FileInfo f in files)
{
lstFiles.Add(sFile(f));
}
}


try
{
directories = di.GetDirectories(searchPattern, SearchOption.TopDirectoryOnly);
}
catch
{
lstFilesErrors.Add(sDir(di));
return;
}

// if that directory has more directories then add them to the list then
// execute this function
if (directories != null)
foreach (DirectoryInfo d in directories)
{
FileInfo[] subFiles = null;
DirectoryInfo[] subDir = null;

bool isThereAnError = false;

try
{
subFiles = d.GetFiles();
subDir = d.GetDirectories();

}
catch
{
isThereAnError = true;
}

if (isThereAnError)
lstFilesErrors.Add(sDir(d));
else
{
lstFiles.Add(sDir(d));
startScan(d);
}


}

}

如果我尝试用类似的方式处理异常,那么问题就来了:

DirectoryInfo di = new DirectoryInfo("A:\\");
FileInfo[] directories = null;
try
{
directories = di.GetFiles("*", SearchOption.AllDirectories);

}
catch (UnauthorizedAccessException e)
{
Console.WriteLine("There was an error with UnauthorizedAccessException");
}
catch
{
Console.WriteLine("There was antother error");
}

是不是如果发生异常我就得不到文件。

最佳答案

这种方法要快得多。一个目录下放很多文件的时候只能tel。我的 A:\外部硬盘驱动器包含近 1 TB,因此在处理大量文件时会产生很大的不同。

static void Main(string[] args)
{
DirectoryInfo di = new DirectoryInfo("A:\\");
FullDirList(di, "*");
Console.WriteLine("Done");
Console.Read();
}

static List<FileInfo> files = new List<FileInfo>(); // List that will hold the files and subfiles in path
static List<DirectoryInfo> folders = new List<DirectoryInfo>(); // List that hold direcotries that cannot be accessed
static void FullDirList(DirectoryInfo dir, string searchPattern)
{
// Console.WriteLine("Directory {0}", dir.FullName);
// list the files
try
{
foreach (FileInfo f in dir.GetFiles(searchPattern))
{
//Console.WriteLine("File {0}", f.FullName);
files.Add(f);
}
}
catch
{
Console.WriteLine("Directory {0} \n could not be accessed!!!!", dir.FullName);
return; // We alredy got an error trying to access dir so dont try to access it again
}

// process each directory
// If I have been able to see the files in the directory I should also be able
// to look at its directories so I dont think I should place this in a try catch block
foreach (DirectoryInfo d in dir.GetDirectories())
{
folders.Add(d);
FullDirList(d, searchPattern);
}

}

顺便说一下,感谢您的评论 Jim Mischel,我得到了这个

关于c# - 快速获取特定路径下的所有文件和目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6061957/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com