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

c#判断文件夹是否存在,不存在创建新的

作者:整整ー世♂陰霾そ   发布日期:2025-07-05   浏览:65

using System;
using System.IO;

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

        // 判断文件夹是否存在,如果不存在则创建
        if (!Directory.Exists(folderPath))
        {
            Directory.CreateDirectory(folderPath);
            Console.WriteLine("文件夹已创建: " + folderPath);
        }
        else
        {
            Console.WriteLine("文件夹已存在: " + folderPath);
        }
    }
}

代码解释:

  1. 引入命名空间:使用 SystemSystem.IO 命名空间,System.IO 提供了对文件和目录进行操作的功能。
  2. 定义文件夹路径:通过 string folderPath 变量定义要检查或创建的文件夹路径。
  3. 判断文件夹是否存在:使用 Directory.Exists(folderPath) 方法来检查指定路径的文件夹是否存在。
  4. 创建文件夹:如果文件夹不存在,则使用 Directory.CreateDirectory(folderPath) 方法创建该文件夹。
  5. 输出信息:根据文件夹是否存在,输出相应的提示信息。

这样可以确保在程序运行时,指定的文件夹存在。如果不存在,则会自动创建。

上一篇:c# const

下一篇:c# async await

大家都在看

c# 二进制

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

.net和c#

c#游戏开发

c#网络编程

c# rectangle

c# 取字符串最后一个字符

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

Laravel 中文站