Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c#创建文件夹

作者:战虎天下   发布日期:2026-03-21   浏览:44

using System;
using System.IO;

class Program
{
    static void Main()
    {
        // 指定要创建的文件夹路径
        string folderPath = @"C:\ExampleFolder";

        try
        {
            // 检查文件夹是否已经存在
            if (!Directory.Exists(folderPath))
            {
                // 创建文件夹
                Directory.CreateDirectory(folderPath);
                Console.WriteLine("文件夹已成功创建: " + folderPath);
            }
            else
            {
                Console.WriteLine("文件夹已经存在: " + folderPath);
            }
        }
        catch (Exception ex)
        {
            // 输出异常信息
            Console.WriteLine("创建文件夹时发生错误: " + ex.Message);
        }
    }
}

解释说明:

  1. 命名空间引用using System;using System.IO; 引入了必要的命名空间,System.IO 提供了对文件和目录进行操作的功能。
  2. 指定文件夹路径string folderPath = @"C:\ExampleFolder"; 定义了要创建的文件夹路径。使用 @ 符号可以避免转义字符的问题。
  3. 检查文件夹是否存在if (!Directory.Exists(folderPath)) 用于检查指定路径的文件夹是否已经存在。
  4. 创建文件夹Directory.CreateDirectory(folderPath); 用于创建文件夹。如果文件夹已经存在,则不会抛出异常。
  5. 异常处理try...catch 块用于捕获并处理可能发生的异常,例如权限不足或路径无效等。
  6. 输出信息:根据文件夹是否存在或创建是否成功,输出相应的提示信息。

希望这段代码和解释对你有帮助!

上一篇:c# required

下一篇:c# 三元运算符

大家都在看

c# 二进制

c# 创建目录

c# datatable group by

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

.net和c#

c#获取系统时间

c#游戏开发

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站