复制文件集时出错

时间:2016-12-22 16:10:37

标签: c# file

我想将一组图像文件从一个路径文件夹复制到另一个文件夹。但是发生了错误:

  

路径中的非法字符

我试过......

string pathImg = "C" + ":\\compaynameSupportFileImg";

if (!Directory.Exists(pathImg))
{
    DirectoryInfo di = Directory.CreateDirectory(pathImg);
    di.Attributes = FileAttributes.Directory | FileAttributes.Hidden;
    string[] jpgFilesNames = Directory.GetFiles(
       @"C:\Program Files (x86)\compayname\name", "*.jpg", SearchOption.AllDirectories);

    string targetDirectoryImg = pathImg + "\\*.jpg";
    foreach (var item in jpgFilesNames)
    {
        File.Copy(item, targetDirectoryImg, true);
    }
}

1 个答案:

答案 0 :(得分:3)

只需使用:

string pathImg = "C" + ":\\compaynameSupportFileImg";
if (!Directory.Exists(pathImg))
{
    DirectoryInfo di = Directory.CreateDirectory(pathImg);
    di.Attributes = FileAttributes.Directory | FileAttributes.Hidden;
    string[] jpgFilesNames = Directory.GetFiles(@"C:\Program Files (x86)\compayname\name", "*.jpg", SearchOption.AllDirectories);
    foreach (var item in jpgFilesNames)
    {
        File.Copy(item, Path.Combine(pathImg, Path.GetFileName(item)), true);
    }
}

此处您不需要targetDirectoryImg。因为它具有C:\compaynameSupportFileImg\*.jpg之类的值,并且它不是目标文件夹的路径