0

So I'm trying to set a custom image for a form application I've made. The images I'm trying to target are in a folder called "Images" on the same level as my Solution file. The solution file is a C# windows forms (net core framework) solution. It's a basic form app that I want to display an image based on a users selection, however right now I get an unhandled exception everytime I try to set the image with this code:

picFood.Image = Image.FromFile("../../Images/burger.jpg");

The exact error is "System.IO.FileNotFoundException: ../../Images/burger.jpg"

In another totally unrelated solution this works. Folder structure is the same. A folder called Images, on the same directory level as the .sln file holds the images there. They're in my solution explorer and everything. I've tried this with one "../" and no "../" as well so I'm not sure what to do from here.

krzychostal
  • 53
  • 1
  • 6
  • The error is clear, the file is not found in the given path. Have you pasted the path into a windows explorer to see if the path indeed contains the specified file? – JohnG Jun 26 '21 at 20:19
  • I guess my question lies in, is there a way to store the file in a subdirectory and call it without using the whole path of where it is? – krzychostal Jun 26 '21 at 20:33
  • If you set the properties of you image files to `Copy Always`, then you will find an `images` folder in the same folder as your EXE. Then you can just refer to it by `.\images\whatever.jpg`, and, when build a deployment package, everything is in the one place – Flydog57 Jun 26 '21 at 21:59

1 Answers1

0

Files with relative paths are opened relative to the working directory of your application.

In this case, when launching from within Visual Studio, the default is the bin folder where the compiled application is put by default.

So if your binary is in <project dir>/bin/Debug/App.exe this path will resolve to <project dir>/Image/burger.jpg.
If you have changed something in your build configuration, or your application switches directory at runtime (e.g. via Directory.SetCurrentDirectory), this path may be different than you expect.

To understand your issue, I suggest you start looking at what your working directory is. You can obtain that in your code via Directory.GetCurrentDirectory().
You can also resolve your relative path using Path.GetFullPath.
Print these two values to see where your program attempts to load the file from.

Keep in mind that any image files you put in the solution/project folder will need to be copied with your binary if you want to use them.
To use relative paths without .. you can copy them alongside your binary during compilation, see: VS2010 How to include files in project, to copy them to build output directory automatically during build or publish and Copying Visual Studio project file(s) to output directory during build for how to do that.

founderio
  • 407
  • 1
  • 3
  • 15
  • Yea I was getting the relative pathing incorrect. I also added them as resources in the resource folder as well. I had them previously in an Images folder, in the same spot as the now resource folder but the same location call ("../../Images/file.jpg") wasn't working, now with them in the resources folder it does work. obviously pointing it to the resources folder instead of the images folder, but it's fine now so I'm happy. – krzychostal Jun 26 '21 at 21:08