文件夹类(Directory)

文件夹的创建

 Directory提供了两个重的CreateDirectory方法声明如下:

使用一个指定的文件夹名创建文件夹,返回一个DirectoryInfo 类实例

Public static DirectoryInfo CreateDirectory( string path)

创建指定路径中的所有文件夹,并应用指定的windows 安全性。返回一个DirectoryInfo实例

Public static DirectoryInfo(string path , DirectorySecurity directorysecurity)

判断指定文件夹是否存在

与判断文件类似,使用Exists方法可以

判断文件夹是否存在,声明如下:

Public static bool Exists( string path)

文件夹的移动和删除

Move 方法用于从一个文件夹移动到另一个文件夹

Delete 放过用于指定删除一个文件,Delete方法重载可以指定否删除子文件夹,

两种方法声明如下

将指定远目录sourceDirName移动到目的的目录destDirName 

Public static void Move( string sourceDirName , string destDirName)

删除指定目录。这个目录必须是空目录,如果有文件,请先清除目录中的文件

Public static void Delete (string path)

删除指定目录,recursive表示 是否递归删除次目录下的子目录以及文件

Public static void Delete(string path ,  bool  recurive)

这个例子首先判断这个文件夹是否存在,如果不存在则创建一个新的文件夹,然后执行文件夹的移动和删除

const string sourecFolderPath = @"F:\lichen";

            const string destFolderPath = @"F:\新建文件夹\li";

            //判断指定的源文件是否存在

            if (!Directory.Exists(sourecFolderPath))

            {

                DirectoryInfo di = Directory.CreateDirectory(sourecFolderPath);//创建一个文件

 

            }

            if (Directory.Exists(sourecFolderPath))

            {

                Directory.Delete(sourecFolderPath, true);

 

            }

            Directory.Move(sourecFolderPath, destFolderPath);//这里的移动只能在一个盘符下,跨盘不能使用

原文地址:https://www.cnblogs.com/lichen396116416/p/1922885.html