sdgfsdh
sdgfsdh

Reputation: 37045

Where should I store application caches on Windows?

I have a Windows application that downloads various files. I would like to cache this files but I am unsure where to put the cache. It should live in the user's home folder, so that they always have access, but I don't think that folders such as Documents are appropriate.

The equivalent of what I am looking for on macOS and Linux are:

Where should application caches be stored on Windows?


Note that unlike in this question, I am not asking about temp files. My cache should be re-used across sessions.


Note that I am not building a Windows Store app.

Upvotes: 3

Views: 1589

Answers (2)

sdgfsdh
sdgfsdh

Reputation: 37045

My final solution:

public static Path getCacheFolder(final String osName, final FileSystem fs) {

    Objects.requireNotNull(osName, "osName is null");
    Objects.requireNotNull(fs, "fs is null");

    // macOS
    if (osName.toLowerCase().contains("mac")) {
        return fs.getPath(System.getProperty("user.home"), "Library", "Caches");
    }

    // Linux
    if (osName.contains("nix") || osName.contains("nux") || osName.contains("aix")) {
        return fs.getPath(System.getProperty("user.home"), ".cache");
    }

    // Windows
    if (osName.contains("indows")) {
        return fs.getPath(System.getenv("LOCALAPPDATA"), "Caches");
    }

    // A reasonable fallback
    return fs.getPath(System.getProperty("user.home"), "caches");
}

Upvotes: 3

Anders
Anders

Reputation: 101606

Per-user data that does not need to roam should be stored under CSIDL_LOCAL_APPDATA. You can get this path by calling SHGetFolderPath (or Environment.GetFolderPath in .NET). Use CSIDL_APPDATA instead if you need the data to roam in domain environments but it is not a good idea for large files...

Upvotes: 2

Related Questions