我使用(alt + 0160)创建了没有名称的文件夹,同时使用c#进行搜索,它停留在无限循环中,并创建了"Stack“例外,给出了我用于搜索的方法。
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);
}
}发布于 2015-12-08 10:17:20
这可以通过简单的更改来避免:在目录枚举代码中,将for循环更改为:
foreach (DirectoryInfo objDir in new DirectoryInfo(objDirs.FullName + Path.DirectorySeparatorChar).GetDirectories(searchPatteren))
{
getTotatFoldersAndFilesCount(objDir, worker);
}枚举空文件夹时,目录名为空白。初始化DirectoryInfo对象时,会对空格进行裁剪,从而使函数始终在同一目录下循环。在大多数情况下添加DirectorySeperatorChar ("\")可以解决这个问题。
发布于 2015-12-09 07:40:35
我搜索这个问题,并通过给定的链接找到解决方案。
通过在目录路径的末尾添加单个斜杠,它将不会进入无限循环。一开始我就是这么做的。
getTotatFoldersAndFilesCount(objDir, worker);现在用这个代替它。它解决了我的问题,
DirectoryInfo nDI = new DirectoryInfo(objDir.FullName + @"\");
getTotatFoldersAndFilesCount(nDI, worker);链接给出了。http://tiku.io/questions/4277530/getdirectories-fails-to-enumerate-subfolders-of-a-folder-with-255-name
https://stackoverflow.com/questions/34149300
复制相似问题