C#删除一个文件夹以及该文件夹内的所有文件和文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2222718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:31:29  来源:igfitidea点击:

C# delete a folder and all files and folders within that folder

c#directory

提问by Jamie

I'm trying to delete a folder and all files and folders within that folder, I'm using the code below and I get the error Folder is not empty, any suggestions on what I can do?

我正在尝试删除一个文件夹以及该文件夹中的所有文件和文件夹,我正在使用下面的代码,但出现错误Folder is not empty,您有什么建议吗?

try
{
  var dir = new DirectoryInfo(@FolderPath);
  dir.Attributes = dir.Attributes & ~FileAttributes.ReadOnly;
  dir.Delete();
  dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[i].Index);
}
catch (IOException ex)
{
  MessageBox.Show(ex.Message);
}

采纳答案by Tommy Carlier

dir.Delete(true); // true => recursive delete

回答by Morfildur

Read the Manual:

阅读手册:

Directory.Delete Method (String, Boolean)

Directory.Delete 方法(字符串,布尔值)

Directory.Delete(folderPath, true);

回答by Paolo Tedesco

The Directory.Deletemethod has a recursive boolean parameter, it should do what you need

Directory.Delete方法具有递归布尔参数,它应该做你需要什么

回答by Dmitri Nesteruk

Err, what about just calling Directory.Delete(path, true);?

呃,只是打电话Directory.Delete(path, true);怎么样?

回答by jinsungy

Try:

尝试:

System.IO.Directory.Delete(path,true)

This will recursively delete all files and folders underneath "path" assuming you have the permissions to do so.

假设您有权限这样做,这将递归删除“路径”下的所有文件和文件夹。

回答by pyrocumulus

You should use:

你应该使用:

dir.Delete(true);

for recursively deleting the contents of that folder too. See MSDN DirectoryInfo.Delete() overloads.

也用于递归删除该文件夹的内容。请参阅 MSDN DirectoryInfo.Delete() 重载

回答by dekdev

public void Empty(System.IO.DirectoryInfo directory)
{
    try
    {
        logger.DebugFormat("Empty directory {0}", directory.FullName);
        foreach (System.IO.FileInfo file in directory.GetFiles()) file.Delete();
        foreach (System.IO.DirectoryInfo subDirectory in directory.GetDirectories()) subDirectory.Delete(true);
    }
    catch (Exception ex)
    {
        ex.Data.Add("directory", Convert.ToString(directory.FullName, CultureInfo.InvariantCulture));

        throw new Exception(string.Format(CultureInfo.InvariantCulture,"Method:{0}", ex.TargetSite), ex);
    }
}

回答by Rosidin Bima

Try this.

尝试这个。

namespace EraseJunkFiles
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo yourRootDir = new DirectoryInfo(@"C:\somedirectory\");
            foreach (DirectoryInfo dir in yourRootDir.GetDirectories())
                    DeleteDirectory(dir.FullName, true);
        }
        public static void DeleteDirectory(string directoryName, bool checkDirectiryExist)
        {
            if (Directory.Exists(directoryName))
                Directory.Delete(directoryName, true);
            else if (checkDirectiryExist)
                throw new SystemException("Directory you want to delete is not exist");
        }
    }
}

回答by Prince

Try This:

尝试这个:

foreach (string files in Directory.GetFiles(SourcePath))
{
   FileInfo fileInfo = new FileInfo(files);
   fileInfo.Delete(); //delete the files first. 
}
Directory.Delete(SourcePath);// delete the directory as it is empty now.