///<summary>
///创建指定目录
///</summary>
///<param name="targetDir"></param>
public void CreateDirectory(string targetDir)
{
DirectoryInfo dir = new DirectoryInfo(targetDir);
if (!dir.Exists)
dir.Create();
}
3、删除文件或目录
///<summary>
///删除指定目录的所有文件和子目录
///</summary>
///<param name="targetDir">操作目录</param>
///<param name="delSubDir">如果为true,包含对子目录的操作</param>
public static void DeleteFiles(string targetDir, bool delSubDir)
{
foreach (string fileName in Directory.GetFiles(targetDir))
{
File.SetAttributes(fileName, FileAttributes.Normal);
File.Delete(fileName);
}
if (delSubDir)
{
DirectoryInfo dir = new DirectoryInfo(targetDir);
foreach (DirectoryInfo subDi in dir.GetDirectories())
{
DeleteFiles(subDi.FullName, true);
subDi.Delete();
}
}
}
///<summary>
///删除指定目录的所有子目录,不包括对当前目录文件的删除
///</summary>
///<param name="targetDir">目录路径</param>
public static void DeleteSubDirectory(string targetDir)
{
foreach (string subDir in Directory.GetDirectories(targetDir))
{
DeleteDirectory(subDir);
}
}
///<summary>
///删除指定目录,包括当前目录和所有子目录和文件
///</summary>
///<param name="targetDir">目录路径</param>
public static void DeleteDirectory(string targetDir)
{
DirectoryInfo dirInfo = new DirectoryInfo(targetDir);
if (dirInfo.Exists)
{
DeleteFiles(targetDir, true);
dirInfo.Delete(true);
}
}