StackOverFlow 与 (alt+0160)

StackOverFlow with (alt+0160)

我使用 (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.WhosebugException 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);
            }

        }

这可以通过简单的更改来避免: 在目录枚举代码中,将for循环改为:

foreach (DirectoryInfo objDir in new DirectoryInfo(objDirs.FullName + Path.DirectorySeparatorChar).GetDirectories(searchPatteren))
{
    getTotatFoldersAndFilesCount(objDir, worker);
}

枚举空白文件夹时,目录名称为白色space。初始化 DirectoryInfo 对象时,whitespace 被修剪,导致函数始终在同一目录中循环。在大多数情况下添加 DirectorySeperatorChar ("\") 可以解决问题。

我google这个问题并通过给定link找到解决方案。

通过在目录路径的末尾添加单斜杠,它不会进入无限循环。 首先我是这样做的。

getTotatFoldersAndFilesCount(objDir, worker);

现在用这个替换它。它解决了我的问题,

 DirectoryInfo nDI = new DirectoryInfo(objDir.FullName + @"\");
                        getTotatFoldersAndFilesCount(nDI, worker);

link 给出。 http://tiku.io/questions/4277530/getdirectories-fails-to-enumerate-subfolders-of-a-folder-with-255-name