Get image path when opening an image with build

I'm trying to build my own image viewer in sdl2. My goal now is when I open a png, the rust build exec needs to open with the image. I can't manage to find a way to get the path of the image that I opened with the executable. If I use current_dir() it gives me the project directory not the image directory.

You need to expand on what you mean/have-coded for middle two sentences. It is not clear if by "build" you are referring to; part of compile step or running the code you have written.

Yes my bad english. By build I mean the executable you get by building your code, which you can open on it s own. For example if you open a file with the .docx extension it opens the file in word. I want to do the same but instead of opening a word file, I want to open an image in my rust app( which is the build). When I do that, the current_dir() returns the project directory not the image directory.

It is up to you to change the working directory (or use a relative path) to open files in a known location. If your program is installed by end users, it is also up to you to install the files in a known location relative to the location of the binary.

If you have a fixed number of files that your app uses, an alternative is to embed the file in your binary.

EDIT: Please ignore this response. I misunderstood the question. I thought it was asking about opening files distributed with the app, but it is not.

Different platforms vary on how they implement “open file with application”, but the current directory is generally not how you do it. On Windows (or Linux) you should find the path of the opened file(s) in std::env::args_os() (after the first element).

3 Likes

Oh thanks, that really helped me.

You should be able to test the functionality without installing it systemwide in the Windows registry by doing this:

Drag the image.PNG, and drop it into your program.EXE .