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

c# 判断文件夹是否存在

作者:Angel★狂龙   发布日期:2026-02-11   浏览:62

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("文件夹不存在。");
        }
    }
}

解释说明:

  1. using System;using System.IO;:引入命名空间,System.IO 提供了用于处理文件和目录的类。
  2. string folderPath = @"C:\ExampleFolder";:定义一个字符串变量 folderPath,表示要检查的文件夹路径。这里使用了 @ 符号来避免转义字符的问题。
  3. Directory.Exists(folderPath):这是一个静态方法,用于检查指定路径的文件夹是否存在。如果存在则返回 true,否则返回 false
  4. if (Directory.Exists(folderPath)):根据 Directory.Exists 的返回值,判断文件夹是否存在,并输出相应的提示信息。

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

上一篇:c#编程软件

下一篇: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 中文站