我正在尝试删除文件夹,但我可以设法正确使用它?
我要删除的文件夹名为 ExtractedFiles ,它位于名为 FormValue 的文件夹中。
我可以删除同一 FormValue 文件夹中的电子表格,但无法删除该文件夹。
我认为问题可能是我没有文件夹
的正确文件扩展名这有效:
if (File.Exists(tempFolderPathAlt + saveas + ".xls"))
{
File.Delete(tempFolderPathAlt + saveas + ".xls");
}
这不起作用:
if (File.Exists(tempFolderPathAlt + "ExtractedFiles"))
{
File.Delete(tempFolderPathAlt + "ExtractedFiles");
}
有人可以告诉我文件夹的文件扩展名或如何删除文件夹吗?
答案 0 :(得分:5)
如果您要删除文件夹,则应使用Directory.Delete
代替File.Delete
:
String path = Path.Combine(tempFolderPathAlt, "ExtractedFiles");
bool directoryExists = Directory.Exists(path);
if(directoryExists)
Directory.Delete(path, true); // deletes sub-directories
答案 1 :(得分:0)
尝试使用Directory.Delete方法。
答案 2 :(得分:0)
你想要
Directory.Delete
由于您删除了文件夹而不是文件
答案 3 :(得分:0)
要删除dirrectories,您需要使用方法
Directory.Delete(string path,
bool recursive);
请参阅官方文档: http://msdn.microsoft.com/en-us/library/fxeahc5f.aspx
答案 4 :(得分:0)
如果您收到IOException删除目录,请检查此信息: