我使用(alt + 0160)创建了没有名称的文件夹,而我用c#搜索它会陷入无限循环并创建" Stack Over Flow" 给出了我用于搜索的方法。
public void getTotatFoldersAndFilesCount(DirectoryInfo objDirs, System.ComponentModel.BackgroundWorker worker)
{
try{
if (worker.CancellationPending == true)
{ return; }
FileInfo[] objFiles = null;
numFoldersCount++;
if ((objDirs.Attributes & FileAttributes.ReparsePoint) != 0)
{ return;}
try
{
objFiles = objDirs.GetFiles(searchPatteren);
}
catch (UnauthorizedAccessException e)
{ }
catch (System.IO.DirectoryNotFoundException e)
{ }
catch (System.StackOverflowException ex)
{ }
if (objFiles != null)
{
foreach (FileInfo objFile in objFiles)
{
numFilesCount++;
}
foreach (DirectoryInfo objDir in objDirs.GetDirectories())
{
getTotatFoldersAndFilesCount(objDir, worker);
}
}
objFiles = null;
}
catch (Exception ex)
{
ErrorLogger("Error in Total Folder and File Count - Directory Name: " + objDirs.Name);
ErrorLogger(ex.Message);
}
}
答案 0 :(得分:1)
这可以通过简单的改变来避免: 在目录枚举代码中,将for循环更改为:
maxValue
枚举空白文件夹时,目录名称为空格。初始化DirectoryInfo对象时,会修剪空白,导致函数始终循环通过同一目录。在大多数情况下添加DirectorySeperatorChar(" \")可以解决问题。
答案 1 :(得分:0)
我谷歌这个问题,并通过给定的链接找到解决方案。
通过在目录路径的末尾添加单斜杠,它将不会进入无限循环。 首先我是这样做的。
getTotatFoldersAndFilesCount(objDir, worker);
现在替换它。它解决了我的问题,
DirectoryInfo nDI = new DirectoryInfo(objDir.FullName + @"\");
getTotatFoldersAndFilesCount(nDI, worker);