Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • ~/Library/Caches/MyApp
  • ~/.cache/MyApp

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.

like image 215
sdgfsdh Avatar asked Jan 21 '26 02:01

sdgfsdh


1 Answers

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");
}
like image 75
sdgfsdh Avatar answered Jan 22 '26 16:01

sdgfsdh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!