您当前的位置: 首页 >  c#

彭世瑜

暂无认证

  • 3浏览

    0关注

    2791博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

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

彭世瑜 发布时间:2017-08-12 11:01:28 ,浏览量:3

  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:文件夹操作之删除
关注
打赏
1665367115
查看更多评论
立即登录/注册

微信扫码登录

0.2188s