且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#编程-113:文件夹操作之创建

更新时间:2022-06-25 03:29:18

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:文件夹操作之创建