What Is a Cross-Platform Way to Get the Home Directory

How can I find the user's home dir in a cross platform manner, using C++?

I don't think it's possible to completely hide the Windows/Unix divide with this one (unless, maybe, Boost has something).

The most portable way would have to be getenv("HOME") on Unix and concatenating the results of getenv("HOMEDRIVE") and getenv("HOMEPATH") on Windows.

How to find user's home directory on Windows and Linux in PHP

After not working on this for a long time, I finally decided to definitely answer this question.

There are some usefull environment variables defined on Windows: USERPROFILE, APPDATA, LOCALAPPDATA. They are easily accessible via getenv() function:

getenv('USERPROFILE');

USERPROFILE exists on any Windows, according to https://learn.microsoft.com/fr-fr/windows/desktop/shell/knownfolderid

So, on Windows, it seems to be reliable.

If you need to store data for the current user, APPDATA and LOCALAPPDATA are good variables to find that place.

I've written a package to make these tools reusable: https://github.com/Arcesilas/Platform

It's still work in progress and certainly needs to be improved. Any help is welcome to make this tool reliable on any platform.

Thanks to eryksun whose comments helped a lot in solving this question.

What is the best way to find the user's home directory in Java?

The bug you reference (bug 4787391) has been fixed in Java 8. Even if you are using an older version of Java, the System.getProperty("user.home") approach is probably still the best. The user.home approach seems to work in a very large number of cases. A 100% bulletproof solution on Windows is hard, because Windows has a shifting concept of what the home directory means.


If user.home isn't good enough for you I would suggest choosing a definition of home directory for windows and using it, getting the appropriate environment variable with System.getenv(String).

What is a cross-platform way to get the home directory?

You want to use os.path.expanduser.

This will ensure it works on all platforms:

from os.path import expanduser
home = expanduser("~")

If you're on Python 3.5+ you can use pathlib.Path.home():

from pathlib import Path
home = str(Path.home())

What is the cross-platform way of obtaining the path to the local application data directory?

You could probably say something like (contradict me if I am wrong, or if this a bad approach)

private String workingDirectory;
//here, we assign the name of the OS, according to Java, to a variable...
private String OS = (System.getProperty("os.name")).toUpperCase();
//to determine what the workingDirectory is.
//if it is some version of Windows
if (OS.contains("WIN"))
{
//it is simply the location of the "AppData" folder
workingDirectory = System.getenv("AppData");
}
//Otherwise, we assume Linux or Mac
else
{
//in either case, we would start in the user's home directory
workingDirectory = System.getProperty("user.home");
//if we are on a Mac, we are not done, we look for "Application Support"
workingDirectory += "/Library/Application Support";
}
//we are now free to set the workingDirectory to the subdirectory that is our
//folder.

Note that, in this code, I am taking full advantage that Java treats '/' the same as '\\' when dealing with directories. Windows uses '\\' as pathSeparator, but it is happy with '/', too. (At least Windows 7 is.) It is also case-insensitive on it's environment variables; we could have just as easily said workingDirectory = System.getenv("APPDATA"); and it would have worked just as well.

Flutter crossplatform storage directory

Try the Path_Provider package: https://pub.dev/packages/path_provider

add this to your pubspec.yaml:

path_provider: ^2.0.11

get default storage:

Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;

Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;

How Can I Find Default User Directories in Elixir

Not fully Elixir, but there is Erlang functions for that - filename:basedir/{2,3}

This function take the type of the directory as a first argument, which is one of the following atoms:

  • user_cache
  • user_config
  • user_data
  • user_log
  • site_config
  • site_data

And application name (also as atom) as a second argument.

Third argument may be a map containing:

  • os - OS type used for the paths creation, there are 3 recognisable values - windows, darwin (macOS), and linux (which will use XDG Base Directory Spec). Any other value will be treated as linux. If not provided, then OS is automatically detected by os:type/0 call.
  • author - used only on Windows
  • version - used only on Windows


Related Topics



Leave a reply



Submit