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

c# directory

作者:月下孤魂   发布日期:2025-03-31   浏览:73

using System;
using System.IO;

class Program
{
    static void Main()
    {
        // 获取当前目录
        string currentDirectory = Directory.GetCurrentDirectory();
        Console.WriteLine("当前目录: " + currentDirectory);

        // 创建新目录
        string newFolderPath = Path.Combine(currentDirectory, "NewFolder");
        if (!Directory.Exists(newFolderPath))
        {
            Directory.CreateDirectory(newFolderPath);
            Console.WriteLine("创建目录: " + newFolderPath);
        }

        // 获取目录中的文件列表
        string[] files = Directory.GetFiles(currentDirectory);
        Console.WriteLine("当前目录中的文件:");
        foreach (string file in files)
        {
            Console.WriteLine(file);
        }

        // 删除目录(如果为空)
        if (Directory.Exists(newFolderPath) && !Directory.EnumerateFiles(newFolderPath).Any())
        {
            Directory.Delete(newFolderPath);
            Console.WriteLine("删除空目录: " + newFolderPath);
        }
    }
}

解释说明:

  1. 获取当前目录:使用 Directory.GetCurrentDirectory() 方法获取当前工作目录的路径。
  2. 创建新目录:使用 Directory.CreateDirectory() 方法创建一个新的目录。在此之前,使用 Directory.Exists() 检查目录是否已经存在。
  3. 获取目录中的文件列表:使用 Directory.GetFiles() 方法获取指定目录中的所有文件,并打印出来。
  4. 删除目录:使用 Directory.Delete() 方法删除指定的目录。在删除之前,确保目录是空的,以避免异常。

如果你需要更多关于 C# 目录操作的帮助,请告诉我!

上一篇:c#字符串转int

下一篇:c# task.delay

大家都在看

c# 二进制

c# tcp client

c# type.gettype

c# sqlconnection

.net和c#

c#游戏开发

c#网络编程

c# rectangle

c# if else

c# rtsp

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

Laravel 中文站