Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I delete a folder and it's contents, without putting them in the recycle bin?

Tags:

c#

file-io

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).

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.

like image 931
Tornado726 Avatar asked Sep 29 '13 05:09

Tornado726


1 Answers

You are looking for FileSystem.DeleteDirectory Method (String, UIOption, RecycleOption)

recycle

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

like image 93
Rahul Tripathi Avatar answered Nov 14 '22 23:11

Rahul Tripathi