1. 首页
  2. 编程语言
  3. C#
  4. C#如何使用代码创建、移动和遍历文件夹

C#如何使用代码创建、移动和遍历文件夹

上传者: 2023-08-25 13:25:46上传 CS文件 3.98KB 热度 10次

在C#中,你可以使用以下代码片段来创建、移动和遍历文件夹。首先,使用Directory类的CreateDirectory方法来创建一个新的文件夹。例如,要在C盘下创建一个名为"NewFolder"的文件夹,可以使用以下代码:

string path = @"C:\NewFolder";
Directory.CreateDirectory(path);

接下来,你可以使用Directory类的Move方法来移动一个文件夹。例如,要将"NewFolder"文件夹移动到D盘下的"NewLocation"文件夹中,可以使用以下代码:

string sourcePath = @"C:\NewFolder";
string destinationPath = @"D:\NewLocation\NewFolder";
Directory.Move(sourcePath, destinationPath);

最后,你可以使用Directory类的GetDirectories方法来遍历一个文件夹中的子文件夹。例如,要获取"C:\NewFolder"文件夹中的所有子文件夹的路径,可以使用以下代码:

string parentFolderPath = @"C:\NewFolder";
string[] subfolderPaths = Directory.GetDirectories(parentFolderPath);

这样,你就可以使用这些代码来在C#中创建、移动和遍历文件夹了。

下载地址
用户评论