C# Stackoverflow中递归删除目录操作

发布于 2024-09-07 16:02:28 字数 1181 浏览 9 评论 0原文

我目前正在重写一个文件删除工具,其中包含安全删除算法和其他内容。 Wenn 我正在尝试递归地浏览目录并删除其中的所有文件和所有子目录等。调试器将抛出 stackoverflow 异常。

   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); }
    }

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); }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇女薄笑 2024-09-14 16:02:28

将此:更改

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

为:

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

Change this:

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

to:

 foreach (string subdir in Directory.GetDirectories(directory))
 {
     deepWipe(subdir);
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文