C# 删除指定目录下的所有文件及文件夹

时间:2021-01-10 12:25:56

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace ConsoleApplication1

{

    class Program

    {

        #region 直接删除指定目录下的所有文件及文件夹(保留目录)

        /// <summary>

        ///直接删除指定目录下的所有文件及文件夹(保留目录)

        /// </summary>

        /// <param name="strPath">文件夹路径</param>

        /// <returns>执行结果</returns>

        public static void DeleteDir(string file)

        {

            try

            {

                //去除文件夹和子文件的只读属性

                //去除文件夹的只读属性

                System.IO.DirectoryInfo fileInfo = new DirectoryInfo(file);

                fileInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;

                //去除文件的只读属性

                System.IO.File.SetAttributes(file, System.IO.FileAttributes.Normal);

                //判断文件夹是否还存在

                if (Directory.Exists(file))

                {

                    foreach (string f in Directory.GetFileSystemEntries(file))

                    {

                        if (File.Exists(f))

                        {

                            //如果有子文件删除文件

                            File.Delete(f);

                            Console.WriteLine(f);

                        }

                        else

                        {

                            //循环递归删除子文件夹

                            DeleteDir(f);

                        }

                    }

                    //删除空文件夹

                    Directory.Delete(file);

                }

            }

            catch (Exception ex) // 异常处理

            {

                Console.WriteLine(ex.Message.ToString());// 异常信息

                

            }

        }

        #endregion

        static void Main(string[] args)

        {

            DeleteDir(@"D:\zhidiangit3.0");

            Console.ReadKey();

        }

 

    }

}