mykds mykds - 3 months ago 11
C# Question

Copy Resource File to Windows dir

I want to copy a resource file to another folder.

System.IO.File.Copy(@"Resources\empty.jpg", destFile);

Thought this might be as easy as that.
But it throws me an error like: "Part of the path Resources\empty.jpg could not be found.

Any help here?


You need to know the file/folder in relation to where you currently are. As Francis said in the comment above, by default in VS you will be in the debug folder. If Resources is off the root of C:, you need to get yourself to the root of C: First somehow, either by referencing C: directly, or by using ..\ to work your way up, parent folder by parent folder.

If it is a sub-directory from where you are at, what you show above does work correctly when the file exists. I tried it myself, which means you are not likely referencing a path that exists within your project\bin\debug path.

If it is a folder off the project, then you need to get up to the project folder itself first, i.e. "..\..\Resources\empty.jog".