如何删除文件夹及其内容,而不将其放入回收站? [英] How do I delete a folder and it's contents, without putting them in the recycle bin?

查看:107
本文介绍了如何删除文件夹及其内容,而不将其放入回收站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个程序来在驱动器上运行文件I/O压力,完成后,我想删除文件,但不想将它们放入回收站.这是我现在拥有的代码:

I am creating a program to run file I/O stress on a drive and when it is done, I want to delete the files, but don't want to put them in the recycle bin. Here is the code I have now:

    public static void DeleteDirectory(string target_dir)
    {
        //Delete the files and folders created during stress.
        string[] files = Directory.GetFiles(target_dir);
        string[] dirs = Directory.GetDirectories(target_dir);
        foreach (string file in files)
        {
            File.SetAttributes(file, FileAttributes.Normal);
            File.Delete(file);
        }
        foreach (string dir in dirs)
        {
            DeleteDirectory(dir);
        }
        Thread.Sleep(30);
        Directory.Delete(target_dir, true);
    }        

但是此代码只是将文件移动到回收站.我不想添加代码以自动清空回收站,因为其中可能有一些我不想删除的文件(这些文件可能属于其他人).

But this code just moves the files to the recycle bin. I don't want to add code to automatically empty the recycle bin as there may be files in there I don't want to delete (the files might belong to someone else).

注意:从到目前为止的答复来看,似乎人们可能会误解我在这里试图做的事情.当我的程序删除文件时,它将文件发送到回收站.我不希望发生这种情况,我想立即删除文件.我没有任何办法告诉系统删除​​文件而不将其发送到回收站.我将永远不需要还原文件.它默认发送到回收站,但是我看不到任何更改方法.请尽可能编辑我发布的代码,以使其立即删除文件.我是C#新手的开始.

NOTE: From the replies this has gotten so far, it seems people might be misunderstanding what I am trying to do here. When my program deletes files, it is sending them to the Recycle Bin. I do NOT want that to happen, I want to delete the files immediately. I do not see any way to tell the system to delete the files WITHOUT sending them to the Recycle Bin. I will never need to restore the files. It is defaulting to sending to the Recycle Bin, but I don't see any way to change this. Please, if possible, edit the code I have posted to have it delete the files immediately. I am a beginning to novice C# user.

推荐答案

您正在寻找

回收

指定是否将已删除的文件发送到 回收站.默认值为RecycleOption.DeletePermanently.

Specifies whether or not the deleted file should be sent to the Recycle Bin. Default is RecycleOption.DeletePermanently.

这篇关于如何删除文件夹及其内容,而不将其放入回收站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆