我有一些文件夹,这些文件夹有一些文本文件,我需要删除这些文件,但我的代码出错!
var dateFolder = Directory.GetDirectories(@"data\stdate").Select(Path.GetDirectoryName).ToArray();
foreach (var dateFile in dateFolder)
{
var stDates =
Directory.GetFiles(@"data\stdate\" + dateFile + "date").Select(Path.GetFileName).ToArray();
foreach (var date in stDates)
{
File.Delete(@"data\stdate\" + dateFile + "date\\" + date);
}
Directory.Delete(@"data\stdate\" + dateFile + "date");
}
答案 0 :(得分:0)
System.IO.DirectoryInfo di = new DirectoryInfo(@"data\stdate");
//This for delete all file in "data\stdate"
foreach (FileInfo file in di.GetFiles())
{
file.Delete();
}
//***************************For delete file in folder
//This for delete all Subfolder and his files in "data\stdate"
foreach (DirectoryInfo dir in di.GetDirectories())
{
dir.Delete(true);
}
//*************************
//This for delete the parent folder "stdate"
di.Delete();
答案 1 :(得分:0)
试试这个:
Directory.Delete("Path", true); //true: It will delete directory by given path, also folders and files in it.
答案 2 :(得分:0)
我删除了原始文件夹,然后再次创建它!
public interface AsyncResponse {
void onSuccess(Object result);
void onError();
}
答案 3 :(得分:0)
我建议你使用系统环境变量,见这里:
原因是,就像您发现的那样:有时可执行文件没有在您编译程序时所期望的目录中运行。
EX:
String query = "%SystemDrive%";
str = Environment.ExpandEnvironmentVariables(query);
Delete(str, true);
这样就保证了可预测的路径而不是相对路径。