Вопрос

I have this code:

var GetDLLFilesForDir = Directory.GetFiles(directory, "*.dll", SearchOption.AllDirectories).Where(s => s.EndsWith(".dll"));
foreach (string DLLFilesForDir in GetDLLFilesForDir)
{
    File.Copy(DLLFilesForDir, Path.Combine(SADIR, DLLFilesForDir), true);
}

But as you can see, when I go to copy it I get an error. I know the error is caused by "DLLFilesForDir", because it is trying to combine a path using "DLLFilesForDir" when I am already using that file.

The problem is, I need the file name to stay the same, so if I changed:

Path.Combine(SADIR, DLLFilesForDir);

To this:

Path.Combine(SADIR, DLLFilesForDir + "1");

Would it change the name of the file being copied because then I'd have a "filename1.dll" instead of "filename.dll" and I need the latter.

Help appreciated, thankyou.

Это было полезно?

Решение

Change your

File.Copy(DLLFilesForDir, Path.Combine(SADIR, DLLFilesForDir), true);

to

File.Copy(DLLFilesForDir, Path.Combine(SADIR, Path.GetFileName(DLLFilesForDir)), true);


From the MSDN page for Path.Combine():

If path2 contains an absolute path, this method returns path2.

So you are trying the copy the file onto itself ^^

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top