C#如何使用代码创建、移动和遍历文件夹
在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#中创建、移动和遍历文件夹了。
下载地址
用户评论