AbhiJA
AbhiJA

Reputation: 341

Error while copying set of files

I want to copy set of image files from one path folder to another folder. But error occurred:

Illegal characters in path

I have tried...

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);
    }
}

Upvotes: 1

Views: 114

Answers (1)

Roman Marusyk
Roman Marusyk

Reputation: 24619

Just use:

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);
    }
}

You do not need targetDirectoryImg here. Because it will has value like C:\compaynameSupportFileImg\*.jpg and it isn't path to target folder

Upvotes: 3

Related Questions