C# 删除文件以及文件夹

代码如下:

 

/// <summary>
        /// 删除文件夹以及文件
        /// </summary>
        /// <param name="directoryPath"> 文件夹路径 </param>
        /// <param name="fileName"> 文件名称 </param>
        public static void DeleteDirectory(string directoryPath, string fileName)
        {

            //删除文件
            for (int i = 0; i < Directory.GetFiles(directoryPath).ToList().Count; i++)
            {
                if (Directory.GetFiles(directoryPath)[i] == fileName)
                {
                    File.Delete(fileName);
                }
            }

            //删除文件夹
            for (int i = 0; i < Directory.GetDirectories(directoryPath).ToList().Count; i++)
            {
                if (Directory.GetDirectories(directoryPath)[i] == fileName)
                {
                    Directory.Delete(fileName, true);
                }
            }
        }

 

调用方式:

删除文件

DeleteDirectory(@"D:\copyfile\zip", @"D:\copyfile\zip\20180916115334.zip");

删除文件夹

DeleteDirectory(@"D:\copyfile\zip", @"D:\copyfile\zip\20180916115334");

参考链接:

https://blog.csdn.net/mysce/article/details/78633459

 

posted @ 2018-09-18 10:33  秋风过、枯叶落  阅读(46765)  评论(0编辑  收藏  举报