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语句块避免文件夹访问被拒绝异常