首页 > 解决方案 > 目标路径“”是目录而不是文件

问题描述

我正在尝试将文件复制到另一个文件夹中,这是我的代码。

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"

标签: c#directory

解决方案


正如错误所说,您希望 targetPath 是文件名。您只是指向一个文件夹。

"C:\\Users\\wout\\source\\repos\\FileCloner\\FileCloner\\bin\\Debug\\net5.0\\Target\\Hypixel Bots Interface\\.gitattributes"

那将指向该目录中的一个新文件


推荐阅读