c#中文件的删除

文件的删除:
 private void DeleteDir(string path)
        {
            if (path.Trim() == "" || !Directory.Exists(path))
                return;
            DirectoryInfo dirInfo = new DirectoryInfo(path);

            FileInfo[] fileInfos = dirInfo.GetFiles();
            if (fileInfos != null && fileInfos.Length > 0)
            {
                foreach (FileInfo fileInfo in fileInfos)
                {
                    //DateTime.Compare( fileInfo.LastWriteTime,DateTime.Now);
                    File.Delete(fileInfo.FullName); //删除文件
                    Console.WriteLine("已删除");
                }
            }

            DirectoryInfo[] dirInfos = dirInfo.GetDirectories();
            if (dirInfos != null && dirInfos.Length > 0)
            {
                foreach (DirectoryInfo childDirInfo in dirInfos)
                {
                    this.DeleteDir(childDirInfo.FullName); //递归
                    Console.WriteLine("已删除");
                }
            }
            Directory.Delete(dirInfo.FullName, true); //删除目录
            Console.WriteLine("已删除");
        }
调用:
                DeleteDir(@"D:\Documents\Visual Studio 2015\Projects\新建文件夹\WindowsFormsApplication1\WindowsFormsApplication1\c");

你可能感兴趣的:(c#)