天天看點

C#程式設計-113:檔案夾操作之建立

VS快速整理層級關系:Ctrl+K+D

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace CreateDirectoryTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = @"C:\Users\pengshiyu\Desktop\test";
 
            if (Directory.Exists(path))
            {
                Console.WriteLine("檔案夾存在,無需建立! " + path);
            }
            else
            {
                //使用try...catch語句避免出現異常
                try
                {
                    Directory.CreateDirectory(path);
                    Console.WriteLine("檔案夾建立成功:" + path);
                }
                catch (Exception ex)
                {
 
                    Console.WriteLine(ex.Message);
                }
            }
            Console.ReadKey();
        }
    }
}      

使用try...catch語句塊避免檔案夾通路被拒絕異常

C#程式設計-113:檔案夾操作之建立