我正在创建一个程序来在驱动器上运行文件 I/O 压力,完成后,我想删除文件,但不想将它们放入回收站。这是我现在的代码:
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);
}
但是这段代码只是将文件移动到回收站。我不想添加自动清空回收站的代码,因为其中可能有我不想删除的文件(这些文件可能属于其他人)。
注意:从目前收到的回复来看,似乎人们可能误解了我在这里尝试做的事情。当我的程序删除文件时,它会将它们发送到回收站。我不希望发生这种情况,我想立即删除这些文件。我没有看到任何方式告诉系统删除文件而不将它们发送到回收站。我永远不需要恢复文件。它默认发送到回收站,但我看不出有什么办法可以改变这一点。如果可能,请编辑我发布的代码,让它立即删除文件。我是 C# 新手。
最佳答案
您正在寻找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.
关于c# - 如何删除文件夹及其内容,而不将它们放入回收站?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19074724/