Getting a path to my running app?

Hello! I’m struggling with getting a path to my running app. What would you recommend to use? And I suppose for Windows and Mac there would be different separators used in the path… So my accompanying question is how to detect, which OS the launched app is running under? Thanks!

So you want to get the file path to the executable that is currently running, did I get it right? The solution would be:

File pathToMyExecutable = File::getSpecialLocation (File::SpecialLocationType::currentExecutableFile);

And that is one of the reasons why you generally want to use the juce File class to handle paths instead of plain strings containing the path. The File class knows your OS, knows how to deal with the specific path separators and has a lot of handy functionality for relative & absolute path handling as well as child and sibling files etc.

2 Likes

@PluginPenguin has good advice regarding the handling of files/filepaths.

As to your follow-on question,

how to detect, which OS the launched app is running under?

You can use the preprocessor for this. JUCE has a set of platform-specific #defines that you can check:

#if JUCE_WINDOWS
  // we're on Windows
#elif JUCE_MAC
  // we're on MacOS
#elif JUCE_LINUX
  // we're on Linux

// etc...
#endif

You can look in ‘juce_TargetPlatform.h’ to see how these are implemented if you need to perform these checks without the JUCE headers included.

Thanks! The input was useful, whoever I should clarify a bit my question.

I’m looking for a class/function, which would return the separator itself (a character) for the current OS. I need it to build my own paths, based on the location, where the running app is placed. Recommended File class deals with existing paths, which is also useful for me. But I’d like to build my own paths as well.

https://docs.juce.com/master/classFile.html#acb780070c018c7a35d1b8694aa72fca2

I’m surprised you didn’t find that with a “juce get separator character” search.

Thanks! That is what I need)

NEVER build your own paths out of strings! It’s a rookie error. Always use something that’s designed for that job, like the File class.

If you search the juce library, you’ll see that we hardly ever use File::getSeparatorChar() because you only need it in very obscure circumstances, and as a user doing normal stuff you should treat it as something to avoid.

File has all the methods you need to manipulate paths for normal purposes, and it’ll make sure they’re always correctly constructed. C++17 also has std::filesystem::path and other new classes for this kind of thing. But doing it with strings is a really bad idea :slight_smile:

2 Likes

I build my own paths to find and use media files that should be located in a specific folder along with running app. And I don’t use any installer to predifine the location of this folder. So that’s why just getting a path with File methods won’t work in my case. I need to build my own path based on the location of my app.

You can build paths with File… for example,

auto appFolder { File::getSpecialLocation (File::userApplicationDataDirectory).getChildFile ("MyApp") };
auto presetsFolder { appFolder.getChildFile ("presets") };

C:\Users\user_name\AppData\Roaming\MyApp\presets

1 Like

I don’t work with known system folder AppData. I need to build path to a custom user folder, which can be anyway.

So my working piece of code is like this now

Blockquote

File pathToMyExecutable = File::getSpecialLocation(File::SpecialLocationType::currentExecutableFile);
auto Newpath = pathToMyExecutable.getParentDirectory().getFullPathName();
Newpath += File::getSeparatorString() + “MyData”+File::getSeparatorString();

And I get the correct path to MyData folder, which is located next to the running app:

D:\juce\MyProject\Builds\VisualStudio2019\x64\Debug\App\MyData

Wouldn’t something like this work for you?

File::getSpecialLocation (File::SpecialLocationType::currentExecutableFile).getSiblingFile (“MyData”);

When you ask a question and experienced people take the time to reply to you and say “hey, you’re asking the wrong question” then you need to listen to them.

You asked about how to get a separator char.

That’s the wrong question.

The right question is “how do I safely concatenate file paths”.

If you just want to ignore our advice and write horrible code that bashes strings together, then fine, good luck, but you’re missing a learning opportunity.

1 Like

I’m ready to learn. Could you please explain me why it’s horrible to concatenate file paths?

a) because you’ll get it wrong!
b) because doing every path manipulation in a way that’s correct on all platforms and which avoids the nasty edge-cases that you won’t have considered, results in much longer, less maintainable, ugly code than just using one of the classes that handles it for you.

Yes, it’s an elegant way. But when I get the path with this code I still need to concatenate a name of a file sitting in MyData folder to work with this particular files.

Could you please then give me an example of how I can get a path to a file placed in MyData folder, which in its turn resides next to my running app?

Look at the File class, read the docs for its methods, and familiarise yourself with what they all do.

If you want examples of how to use them, go and search the repo or google it: if you search for “getChildFile” or “File::getSpecialLocation”, you’ll find literally hundreds of different examples.

1 Like
File::getSpecialLocation (File::SpecialLocationType::currentExecutableFile).getSiblingFile ("MyData").getChildFile ("MySpecialFile");

This code returns me the whole File, not just mere string. So in order to extract path as a string I have to apply also an additional getFullPathName. But anyway thanks!

I’m getting separator with getSeparatorString, which belongs to File and is trustful, right? So why simple concatenating after that must be avoided? I’ve been using this approach many times in other cross-platform IDE and it worked just fine.