c# - 删除文件夹 C# .net Core
问题描述
我无法删除包含所有文件的文件夹。我收到此错误:
Could not find a part of the path
我想要完成的是,从数据库中获取相对路径,然后删除该文件夹中的所有文件。
这是代码:
public IActionResult RemoveCar(string item)
{
var car = _context.CarModels.Where(x => x.Id.ToString() == item).FirstOrDefault();
var pictures = _context.Pictures.Where(x => x.CarModelId.ToString() == item).ToList();
if(pictures.Count() > 0 && pictures != null)
{
string parent = new System.IO.DirectoryInfo(pictures[0].Path).Parent.ToString();
string lastFolderName = Path.GetFileName(Path.GetDirectoryName(parent+"/"));
string exactPath = Path.GetFullPath("/images/" + lastFolderName);
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(exactPath);
// Delete this dir and all subdirs.
try
{
di.Delete(true);
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
}
foreach (var pic in pictures)
{
_context.Pictures.Remove(pic);
}
}
_context.CarModels.Remove(car);
return RedirectToAction("RemoveCar");
}
解决方案
我认为这行的第一个斜线是问题所在,
string exactPath = Path.GetFullPath("/images/" + lastFolderName);
因为它的意思是“移到根”。如果您想要相对路径,请将其排除在外。
推荐阅读
- visual-studio-code - VSCode 终端中的 PHP 版本/可执行文件错误,但在 Mac 终端中完美运行
- alexa - Alexa Direct Skill Connection 触发 Apple Music?
- terraform - Terraform 为每个计数资源迭代地图
- discord - 如何让我的不和谐机器人从短语列表中选择一条消息,然后从该列表中随机选择一个短语发送给特定用户?
- aws-amplify - 如何增加 aws amplify 构建超时?
- python - 如何在python的文本文件中提取两个字符串之间的值?
- java - Android - 像谷歌地图一样准确获取坐标
- netezza - Netezza 中的 GROUP_CONCAT 替代方案?
- sql - Rails 在带有连接的 where 子句中运行 AND 查询
- python-3.x - 如何处理 exif 图像对象中缺少的 .gps_latitude 属性?