Self app directory

Is there a cross-platform way of determining 1) the app (the one that is being compiled) internal directory, 2) the app storage directory and 3) the app cache directory? I found the crate directories, but it requires the app name to retrieve the respective directories from (link).

I want to mimmick Adobe AIR API at this point: flash.filesystem.File.applicationDirectory and flash.filesystem.File.applicationStorageDirectory and resolve the app: scheme.

Wouldn't something along the lines of std::fs::canonicalize(".") work?

This will return something like the executable path, right? What I'm looking for is the path of the app internal and storage directory, not the path of where it was installed or ran. For example, on Android, if I recall correctly, the app directory is usually at file://storage/emulated/0/Android/, while on Windows it is usually located at AppData/Roaming/....

I mistyped at this part, so I fixed the original post.

It sounds like you are looking for the dirs crate.

Also keep in mind that different OSs will have different well-known folders. For example, the dirs::runtime_dir() function will return None for anything except Linux because /run/user/ is a Linux-only concept.

1 Like

Thanks, unfortunately these functions return user-based directory. I want to know the application directories, like it's possible in Adobe AIR above.

For example, dirs::data_dir() in Windows returns C:\Users\B\AppData\Roaming, but I want something like C:\Users\B\AppData\Roaming\MyFooApp.

Plainly, this is not a facility provided by every operating system. You will need to choose something to do with what's already there.

iOS and Android are the only platforms I'm aware of that actually provide the function you're asking for, and that's because they made it mandatory.

For data_dir, on all platforms you want to add another folder component after what it gives you with your application name.

1 Like

In this case, is it possible to have a multi-thread static variable like follows in a library?

static mut APP_ID: &'static str = "";

So that I can change APP_ID later to something like "" through my app? I want this to avoid giving parameters to my library's structure.

Btw, this'd work, but it'd be good if APP_ID was determined automatically.

Try a #[link_name] const &'static str and see if it works?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.