stirng winPath = "D:\\TargetDirectory”;
stirng macPath = "/Users/用户名/Desktop/TargetDirectory”;
public static void ClearAllFiles (string path)
{
try
{
if(Directory.Exists(path))
{
/** 删除文件夹下所有文件 */
//方法一:
//Directory.GetFiles(path).ToList().ForEach(
//a => File.Delete(a));
//方法二:
//DirectoryInfo dirInfo = new DirectoryInfo(path);
//dirInfo.GetFiles().ToList().ForEach(a => a.Delete());
//方法三:
//DirectoryInfo dirInfo = new DirectoryInfo(path);
//dirInfo.GetFiles().ToList().ForEach(
//a => File.Delete(a.FullName));
/** 删除文件夹下所有文件夹 */
//方法一:
//Directory.GetDirectories(path).ToList().ForEach(
//a => Directory.Delete(a, true));
//方法二:
//DirectoryInfo dirInfo = new DirectoryInfo(path);
//dirInfo.GetDirectories().ToList().ForEach(a => a.Delete());
//方法三:
//DirectoryInfo dirInfo = new DirectoryInfo(path);
//dirInfo.GetDirectories().ToList().ForEach(
//a => Directory.Delete(a.FullName, true));
/** 删除文件夹下所有文件与文件夹 */
DirectoryInfo dirInfo = new DirectoryInfo(path);
FileSystemInfo[] fileSysInfo = dirInfo.GetFileSystemInfos();
foreach(FileSystemInfo fsi in fileSysInfo)
{
if(fsi is DirectoryInfo)
{
Directory.Delete(fsi.FullName, true);
}
else
{
File.Delete(fsi.FullName);
}
}
}
else
{
Directory.CreateDirectory(path);
}
}
catch(Exception e)
{
Debug.Log("Exception: " + e.ToString());
}
}
注: FileInfo与DirectoryInfo都继承自抽象类FileSystemInfo。
网友评论