提问人:ismail temel 提问时间:1/10/2023 最后编辑:ismail temel 更新时间:1/11/2023 访问量:80
我在以下代码行 DirectoryNotFoundException 中收到此错误:找不到路径的一部分
I am getting this error in the following lines of code DirectoryNotFoundException: Could not find a part of the path
问:
在这里,我从 using 行收到错误
using (var filestream = new FileStream(fullPath, FileMode.Create))
{
await file.CopyToAsync(filestream).ConfigureAwait(false);
}
names.Add(name);
在这里,我在 if 块中遇到错误。
if (files.Any())
{
var names = await FileTransactionManager.SaveFileAsync(files, path2);
var ancients = JsonConvert.DeserializeObject<string[]>(getEntity.NAME);
names.AddRange(ancients);
getEntity.NAME = JsonConvert.SerializeObject(names.ToArray());
}
如果我需要解释一般的问题,我在尝试在程序中添加文件时会遇到错误。错误的原因在我描述的代码行中。
这就是整个错误消息 DirectoryNotFoundException:找不到路径“C:\Users\muhasebe\Documents\GitHub\BO_Sleader2\CoreFramework.SLeader\wwwroot\klasorler\Fiyat_Listeleri\294_Haftalik_Rapor14.pdf”的一部分。
有谁知道为什么我会通过这些代码收到此错误?
答:
1赞
Yanga
1/11/2023
#1
检查目录是否存在:“C:\Users\muhasebe\Documents\GitHub\BO_Sleader2\CoreFramework.SLeader\wwwroot\klasorler\Fiyat_Listeleri” 如果没有,请创建它!
string path = @"C:\Users\muhasebe\Documents\GitHub\BO_Sleader2\CoreFramework.SLeader\wwwroot\klasorler\Fiyat_Listeleri\";
// or
// string path = Path.Combine(_hostingEnvironment.WebRootPath, "klasorler\Fiyat_Listeleri")
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
评论
1赞
Yanga
1/11/2023
您必须指定文件夹,看起来 path2 在您的代码中不正确:var names = await FileTransactionManager.SaveFileAsync(files, path2);
0赞
ismail temel
1/11/2023
那么,我会在控制器中还是在我制作的 filetransaction 的类中使用您在上面代码中共享的代码,我问了很多问题,但再次感谢您。
0赞
Yanga
1/11/2023
它应该进入控制器,如果它不存在,则创建“path2”
评论