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);
}
}
}
using System; 和 using System.IO; 引入了必要的命名空间,System.IO 提供了对文件和目录进行操作的功能。string folderPath = @"C:\ExampleFolder"; 定义了要创建的文件夹路径。使用 @ 符号可以避免转义字符的问题。if (!Directory.Exists(folderPath)) 用于检查指定路径的文件夹是否已经存在。Directory.CreateDirectory(folderPath); 用于创建文件夹。如果文件夹已经存在,则不会抛出异常。try...catch 块用于捕获并处理可能发生的异常,例如权限不足或路径无效等。希望这段代码和解释对你有帮助!
上一篇:c# required
下一篇:c# 三元运算符
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站