如何在Java中获取本地应用程序数据文件夹? [重复]

[英]How to get local application data folder in Java? [duplicate]


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

可能重复:获取本地应用程序数据目录路径的跨平台方式是什么?

I'm looking for a way to get the location of the local application data folder, which is a special Windows folder, in Java. Unfortunately, the following only works for English versions of Windows XP with default settings:

我正在寻找一种方法来获取本地应用程序数据文件夹的位置,这是一个特殊的Windows文件夹,用Java。遗憾的是,以下内容仅适用于具有默认设置的英文版Windows XP:

System.getProperty("user.home") + "\\Local Settings\\Application Data"

What I'd like to have is something like this in .NET:

我希望在.NET中拥有这样的东西:

System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)

Is there a way to do it without having to call SHGetSpecialFolderLocation of the Windows Shell API?

有没有办法在不必调用Windows Shell API的SHGetSpecialFolderLocation的情况下执行此操作?

7 个解决方案

#1


10  

Reading the "Shell Folders" registry key is deprecated starting from Windows 95. The registry key contains a note saying "!Do not use this registry key. Use the SHGetFolderPath or SHGetKnownFolderPath instead." I had to find this out the hard way on a Vista system where all the keys were missing except for the warning note.

从Windows 95开始,不推荐读取“Shell文件夹”注册表项。注册表项包含一条说明“!不要使用此注册表项。请改用SHGetFolderPath或SHGetKnownFolderPath。”我不得不在Vista系统上发现这一点,除了警告说明之外,所有键都丢失了。

This related stackoverflow answer solves this problem on Windows using JNA, which is the solution I'm currently using.

这个相关的stackoverflow答案在Windows上使用JNA解决了这个问题,这是我目前正在使用的解决方案。

#2


72  

System.getenv("APPDATA")

(there seems to be no env variable for the "Local Settings" folder, but this will give you the 'Application Data' folder)

(“本地设置”文件夹似乎没有env变量,但这会为您提供“应用程序数据”文件夹)

#3


9  

what about the following

以下是什么

String dataFolder = System.getenv("LOCALAPPDATA");

I have a situation where this is NOT under "user.home"

我有一种情况,这不是在“user.home”

#4


3  

I would like to use the following two ways:

我想用以下两种方式:

String dataFolder = System.getenv("APPDATA");

String dataFolder = System.getProperty("user.home") + "\\Local Settings\\ApplicationData";

#5


1  

You could read the path from the registry: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\* where * is one of those Keys:

您可以从注册表中读取路径:HKEY_CURRENT_USER \ Software \ Microsoft \ Windows \ CurrentVersion \ Explorer \ Shell Folders \ *其中*是其中一个键:

  • Local AppData (C:\Documents and Settings\USER\Local Settings\Application Data)
  • 本地AppData(C:\ Documents and Settings \ USER \ Local Settings \ Application Data)
  • Local Settings (C:\Documents and Settings\USER\Local Settings)
  • 本地设置(C:\ Documents and Settings \ USER \ Local Settings)
  • AppData (C:\Documents and Settings\USER\Application Data)
  • AppData(C:\ Documents and Settings \ USER \ Application Data)

Note: Those example paths are from an english Windows XP installation

注意:这些示例路径来自英语Windows XP安装

#6


0  

I resolved in this way

我这样解决了

private static File getAppData(){
    ProcessBuilder builder = new ProcessBuilder(new String[]{"cmd", "/C echo %APPDATA%"});

    BufferedReader br = null;
    try {
        Process start = builder.start();
        br = new BufferedReader(new InputStreamReader(start.getInputStream()));
        String path = br.readLine();
        // TODO HACK do not know why but I get an extra '"' at the end
        if(path.endsWith("\"")){
            path = path.substring(0, path.length()-1);
        }
        return new File(path.trim());


    } catch (IOException ex) {
        Logger.getLogger(Util.class.getName()).log(Level.SEVERE, "Cannot get Application Data Folder", ex);
    } finally {
        if(br != null){
            try {
                br.close();
            } catch (IOException ex) {
                Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    return null;
}

#7


-1  

It would be possible to spawn a process to query the key and then parse the output:

可以生成一个进程来查询键,然后解析输出:

REG QUERY "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Local AppData"

Honestly, though, I'd be more inclined to use JNA or JNI.

但老实说,我更倾向于使用JNA或JNI。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2009/07/29/ec304d0c9c546d038f957a539916edde.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号