C# How to copy a file without changing the original file name

user2784566

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.

Raidri supports Monica

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 ^^

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

replacing filenames with parent folder name without changing file endings

From Dev

Changing a file name, without rewrite it

From Dev

Copying lists: editing copy without changing original

From Dev

How to download file with its original name instead of unique name?

From Dev

How to copy a file line by line keeping its original line breaks

From Dev

@MultipartForm How to get the original file name?

From Dev

Linux: changing file ownership without a copy?

From Dev

How to get the original file name when downloading file with java

From Dev

How do I manipulate a copy of NSDictionary's contents without changing the contents of original

From Dev

Copy, Create and Update a file, keeping the original file in C#

From Dev

How to address changing, generated file name to variable?

From Dev

How to copy array without changing original array?

From Dev

How to split log file into two without keeping the original file?

From Dev

How can I manipulate a copy of an array without changing the original array?

From Dev

How to copy a file within its original directory, most easily?

From Dev

Copy a file and keep the same timestamp of the original file

From Dev

How to copy whole file from command line without changing destination creation time

From Dev

Copying lists: editing copy without changing original

From Dev

How to download file with its original name instead of unique name?

From Dev

How to copy a file line by line keeping its original line breaks

From Dev

How to copy a file in C?

From Dev

Copy File Name to New File

From Dev

How to get the original file name of a module in PyKD?

From Dev

Copy, Create and Update a file, keeping the original file in C#

From Dev

How to copy a file in a particular directory without knowing the end name, from a docker container (glob not working)

From Dev

C# How to Rename file after File.Copy without using File.Move

From Dev

Copy file name without extension in Vifm

From Dev

Changing file name

From Dev

Unix: copy a file with original permission in C

Related Related

  1. 1

    replacing filenames with parent folder name without changing file endings

  2. 2

    Changing a file name, without rewrite it

  3. 3

    Copying lists: editing copy without changing original

  4. 4

    How to download file with its original name instead of unique name?

  5. 5

    How to copy a file line by line keeping its original line breaks

  6. 6

    @MultipartForm How to get the original file name?

  7. 7

    Linux: changing file ownership without a copy?

  8. 8

    How to get the original file name when downloading file with java

  9. 9

    How do I manipulate a copy of NSDictionary's contents without changing the contents of original

  10. 10

    Copy, Create and Update a file, keeping the original file in C#

  11. 11

    How to address changing, generated file name to variable?

  12. 12

    How to copy array without changing original array?

  13. 13

    How to split log file into two without keeping the original file?

  14. 14

    How can I manipulate a copy of an array without changing the original array?

  15. 15

    How to copy a file within its original directory, most easily?

  16. 16

    Copy a file and keep the same timestamp of the original file

  17. 17

    How to copy whole file from command line without changing destination creation time

  18. 18

    Copying lists: editing copy without changing original

  19. 19

    How to download file with its original name instead of unique name?

  20. 20

    How to copy a file line by line keeping its original line breaks

  21. 21

    How to copy a file in C?

  22. 22

    Copy File Name to New File

  23. 23

    How to get the original file name of a module in PyKD?

  24. 24

    Copy, Create and Update a file, keeping the original file in C#

  25. 25

    How to copy a file in a particular directory without knowing the end name, from a docker container (glob not working)

  26. 26

    C# How to Rename file after File.Copy without using File.Move

  27. 27

    Copy file name without extension in Vifm

  28. 28

    Changing file name

  29. 29

    Unix: copy a file with original permission in C

HotTag

Archive