Moving File to another folder

I have this:

File.Move(file, trashFolderPath + "\\" + file);

where file is some value like:

C:\mytest\Images\Hannah, Pow, 199169, 211 Addendum.pdf

and second parameter all together has a value like:

"C:\\mytest\\ImagesNotFound\\C:\\mytest\\Images\\Hannah, Pow, 199169, 211 Addendum.pdf"

But I get this exception:

The given path's format is not supported.

Upvotes: 0

Views: 82

Answers (2)

mason
mason

Reputation: 32719

"C:\mytest\ImagesNotFound\C:\mytest\Images\Hannah, Pow, 199169, 211 Addendum.pdf" is not a valid file path. So you need to get the file name from file, then append that to trashFolderPath.

File.Move(file, Path.Combine(trashFolderPath, Path.GetFileName(file)); 

Use Path.Combine() to combine path names. It automatically uses the appropriate directory separator, so your code is more portable.

Upvotes: 2

Habib
Habib

Reputation: 223402

You are using Full file name, that includes the complete path and that is what is being used for Target path. Notice the directory letter C:. Use:

Path.GetFileName

File.Move(file, trashFolderPath + "\\" + Path.GetFileName(file));

You may also use Path.Combine instead of concatenating paths like:

File.Move(file, Path.Combine(trashFolderPath,Path.GetFileName(file)));

Upvotes: 4

Related Questions