开发者

C# Stackoverflow in recursive deleting directory operation

开发者 https://www.devze.com 2023-01-05 18:58 出处:网络
I\'m currently rewriting a file deletion tool with safe deleting algorithm and stuff. Wenn I\'m trying to browse through a directory recursivly and to delete all files in there and all subdirs etc. th

I'm currently rewriting a file deletion tool with safe deleting algorithm and stuff. Wenn I'm trying to browse through a directory recursivly and to delete all files in there and all subdirs etc. the debugger will throw a stackoverflow exception.

   private void wipeFile(string file)
   {
        bool ret = false;
        switch (m_algo)
        {
            case Algorithms.fastAlgo:
                ret = FastWipe.WipeFile(file);
                break;
            case Algorithms.safeAlgo:
                ret = CleanWipe.WipeFile(file, m_timesToWrite);
                break;
        }
        handleFileWiped(file, DateTime.Now, ret);
    }

    /// <summary>
    /// Wipes a directory recursively
    /// </summary>
    /// <param name="directory">Given subdir</param>
  开发者_高级运维  private void deepWipe(string directory)
    {
        foreach (string file in Directory.GetFiles(directory))
        {
            wipeFile(file);
        }
        foreach (string subdir in Directory.GetDirectories(directory))
        {
            deepWipe(directory);
        }
        try
        {
            Directory.Delete(directory);
            handleDirectoryWiped(directory, DateTime.Now, true);
        }
        catch { handleDirectoryWiped(directory, DateTime.Now, false); }
    }


Change this:

 foreach (string subdir in Directory.GetDirectories(directory))
 {
     deepWipe(directory);
 }

to:

 foreach (string subdir in Directory.GetDirectories(directory))
 {
     deepWipe(subdir);
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号