c# - 目标路径“”是目录而不是文件
问题描述
我正在尝试将文件复制到另一个文件夹中,这是我的代码。
private static void CopyDirectory(DirectoryInfo source, DirectoryInfo target)
{
foreach (var sourceFilePath in Directory.GetFiles(source.FullName))
{
FileInfo file;
if(IsAccesable(sourceFilePath, out file))
{
file.CopyTo(target.FullName); //ERROR HERE
}
else
{
//DO WORKAROUND
}
}
foreach(var sourceSubDirectoryPath in Directory.GetDirectories(source.FullName))
{
DirectoryInfo directory;
if(IsAccesable(sourceSubDirectoryPath, out directory))
{
var targetSubDictionary = target.CreateSubdirectory(Path.Combine(target.FullName, sourceSubDirectoryPath));
CopyDirectory(directory, targetSubDictionary);
}
else
{
//DO WORKAROUND
}
}
}
我不断收到错误消息:目标路径“”是目录而不是文件
完整的源路径:
"c:\\Hypixel Bots Interface\\.gitattributes"
完整的目标路径:
"C:\\Users\\wout\\source\\repos\\FileCloner\\FileCloner\\bin\\Debug\\net5.0\\Target\\Hypixel Bots Interface"
解决方案
正如错误所说,您希望 targetPath 是文件名。您只是指向一个文件夹。
"C:\\Users\\wout\\source\\repos\\FileCloner\\FileCloner\\bin\\Debug\\net5.0\\Target\\Hypixel Bots Interface\\.gitattributes"
那将指向该目录中的一个新文件
推荐阅读
- python - 如何正确使用带有 level 和 axis 参数的 pandas sort_index?
- php - 从搜索控制器中删除所需的属性 - 非对象的属性
- gcc - 如何使用 MinGW 访问 c 代码中的链接器符号?
- google-apps-script - 在 Google Sheet/Google App Script 中,当我编辑一个单元格时,我想在另一个单元格上调用函数,但不工作
- mysql - mysql insert into 不工作,但 select 不工作
- r - ggplot 到 ggplotly 不适用于自定义 geom_boxplot 宽度
- amazon-s3 - Presto 无法从 S3 导入 PARQUET 文件
- c++ - C++:如何从 make_shared 部分推断模板参数
- c++ - 如何使用 Windows 媒体 API(如 Media Foundation 或 DirectShow)检测相机帧丢失?
- sql-server - 存储过程导致 100% CPU 使用率