VS快速整理层级关系:Ctrl+K+D

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. namespace CreateDirectoryTest
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             string path = @"C:\Users\pengshiyu\Desktop\test";
  13.  
  14.             if (Directory.Exists(path))
  15.             {
  16.                 Console.WriteLine("文件夹存在,无需创建! " + path);
  17.             }
  18.             else
  19.             {
  20.                 //使用try...catch语句避免出现异常
  21.                 try
  22.                 {
  23.                     Directory.CreateDirectory(path);
  24.                     Console.WriteLine("文件夹创建成功:" + path);
  25.                 }
  26.                 catch (Exception ex)
  27.                 {
  28.  
  29.                     Console.WriteLine(ex.Message);
  30.                 }
  31.             }
  32.             Console.ReadKey();
  33.         }
  34.     }
  35. }

使用try...catch语句块避免文件夹访问被拒绝异常
C#编程-113:文件夹操作之创建_彭世瑜_新浪博客_.net