C#编程-114:文件夹操作之删除

84 阅读1分钟
  1. using System;

  2. using System.Collections.Generic;

  3. using System.IO;

  4. using System.Linq;

  5. using System.Text;

  6.  

  7. namespace DeleteDirectoryTest

  8. {

  9.     class Program

  10.     {

  11.         static void Main(string[] args)

  12.         {

  13.             string path = @"C:\Users\pengshiyu\Desktop\test";

  14.  

  15.             if (Directory.Exists(path))

  16.             {

  17.                 try

  18.                 {

  19.                     Console.WriteLine("请输入删除方式:\n1、当文件夹为空时删除,\n2、删除所有子目录");

  20.                     string choice = Console.ReadLine();

  21.                     if (choice == "1")

  22.                     {

  23.                         Directory.Delete(path, false);//如果文件夹包涵子目录,则抛出异常

  24.                         Console.WriteLine("删除空文件成功");

  25.                     }

  26.                     else if (choice == "2")

  27.                     {

  28.                         Directory.Delete(path,true);

  29.                         Console.WriteLine("删除所有文件成功");

  30.                     }

  31.                     else

  32.                     {

  33.                         Console.WriteLine("用户输入有误!");

  34.                     }

  35.                 }

  36.                 catch (Exception ex)

  37.                 {

  38.                     Console.WriteLine(ex.Message);

  39.                 }

  40.             }

  41.             else

  42.             {

  43.                 Console.WriteLine("文件不存在!" + path);

  44.             }

  45.             Console.ReadKey();

  46.         }

  47.     }

  48. }

\

\

\

recursive 英[rɪˈkɜ:sɪv] 美[rɪˈkɜ:rsɪv]

adj. 回归的,递归的;

\

C#编程-114:文件夹操作之删除
\