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. if (Directory.Exists(path))
  14. {
  15. Console.WriteLine("文件夹存在,无需创建! " + path);
  16. }
  17. else
  18. {
  19. //使用try...catch语句避免出现异常
  20. try
  21. {
  22. Directory.CreateDirectory(path);
  23. Console.WriteLine("文件夹创建成功:" + path);
  24. }
  25. catch (Exception ex)
  26. {
  27. Console.WriteLine(ex.Message);
  28. }
  29. }
  30. Console.ReadKey();
  31. }
  32. }
  33. }


使用try...catch语句块避免文件夹访问被拒绝异常 ​​