简体   繁体   中英

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.

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...

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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM