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);
}
}
}
Directory.GetCurrentDirectory()
方法获取当前工作目录的路径。Directory.CreateDirectory()
方法创建一个新的目录。在此之前,使用 Directory.Exists()
检查目录是否已经存在。Directory.GetFiles()
方法获取指定目录中的所有文件,并打印出来。Directory.Delete()
方法删除指定的目录。在删除之前,确保目录是空的,以避免异常。如果你需要更多关于 C# 目录操作的帮助,请告诉我!
上一篇:c#字符串转int
下一篇:c# task.delay
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站