using System;
using System.IO;
class Program
{
static void Main()
{
// 定义文件夹路径
string folderPath = @"C:\ExampleFolder";
// 使用 Directory.Exists 方法判断文件夹是否存在
if (Directory.Exists(folderPath))
{
Console.WriteLine("文件夹存在。");
}
else
{
Console.WriteLine("文件夹不存在。");
}
}
}
using System; 和 using System.IO;:引入命名空间,System.IO 提供了用于处理文件和目录的类。string folderPath = @"C:\ExampleFolder";:定义一个字符串变量 folderPath,表示要检查的文件夹路径。这里使用了 @ 符号来避免转义字符的问题。Directory.Exists(folderPath):这是一个静态方法,用于检查指定路径的文件夹是否存在。如果存在则返回 true,否则返回 false。if (Directory.Exists(folderPath)):根据 Directory.Exists 的返回值,判断文件夹是否存在,并输出相应的提示信息。希望这段代码和解释对你有帮助!
上一篇:c#编程软件
下一篇:c# 析构函数
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站