天天看點

C#程式設計-114:檔案夾操作之删除

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
 
namespace DeleteDirectoryTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = @"C:\Users\pengshiyu\Desktop\test";
 
            if (Directory.Exists(path))
            {
                try
                {
                    Console.WriteLine("請輸入删除方式:\n1、當檔案夾為空時删除,\n2、删除所有子目錄");
                    string choice = Console.ReadLine();
                    if (choice == "1")
                    {
                        Directory.Delete(path, false);//如果檔案夾包涵子目錄,則抛出異常
                        Console.WriteLine("删除空檔案成功");
                    }
                    else if (choice == "2")
                    {
                        Directory.Delete(path,true);
                        Console.WriteLine("删除所有檔案成功");
                    }
                    else
                    {
                        Console.WriteLine("使用者輸入有誤!");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            else
            {
                Console.WriteLine("檔案不存在!" + path);
            }
            Console.ReadKey();
        }
    }
}      

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

adj. 回歸的,遞歸的;

C#程式設計-114:檔案夾操作之删除