使用 Java 在 Windows 上查找用户的“我的文档”文件夹的正确方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4114543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:34:04  来源:igfitidea点击:

Proper method to find user's My Documents folder on Windows with Java?

javawindowsregistrydocuments

提问by Aleksei Vasiliev

For whatever reason, I sometimes need to find the current user's My Documents folder on Windows in a Java program to read some files. But as far as I can tell, there is no way to do it that isn't severely flawed.

无论出于何种原因,我有时需要在 Java 程序中找到当前用户在 Windows 上的“我的文档”文件夹以读取一些文件。但据我所知,没有办法做到不存在严重缺陷。

The first wrong way: System.getProperty("user.home");
Why it won't work:

第一种错误方式:System.getProperty("user.home");
为什么它不起作用:

  • It only returns the \username\ folder; I'd need to add "\Documents\" on to the end to get the Documents folder... and that only works in English.
  • Sun bugs 6519127and 4787931. Java finds the user home folder on Windows by reading a deprecated registry key* to find the Desktop then taking the parent; this method has multiple known problems that will easily cause a completely wrong folder to be returned. The bugs are 3.75 years and 8 years old with no fix.
  • 它只返回 \username\ 文件夹;我需要在末尾添加“\Documents\”以获取 Documents 文件夹......而且这只适用于英文。
  • Sun 错误 6519127和 4787931。Java 通过读取已弃用的注册表项* 来查找桌面,然后获取父文件夹,从而在 Windows 上找到用户主文件夹;此方法存在多个已知问题,很容易导致返回完全错误的文件夹。这些错误是 3.75 年和 8 年,没有修复。

The second wrong way: Using a registry-reading program to get the Personal folder of the user, which is My Documents (but i18n'd).
Why it won't work:
While it fixes the English-only problem, it's still using the same deprecated registry area, so the bugs still apply to it.

第二种错误方式:使用注册表读取程序获取用户的个人文件夹,即我的文档(但已被输入)。
为什么它不起作用:
虽然它修复了仅限英文的问题,但它仍然使用相同的弃用注册表区域,因此错误仍然适用于它。

The deprecated registry key says to use a native call (SHGetKnownFolderPath) which I obviously can't do from Java.

已弃用的注册表项表示使用本机调用 (SHGetKnownFolderPath),我显然无法从 Java 中执行此操作。

The third wrong way:

第三种错误方式:

JFileChooser fr = new JFileChooser();  
FileSystemView fw = fr.getFileSystemView();  
File documents = fw.getDefaultDirectory();

Why it won't work: It works great!
Except when it doesn't. While I had a program that used this open and running in the background, I opened a DirectX game (Fallout: New Vegas). The Java program immediately terminatedwith no stack trace. Always reproducible (for me on that game, and who knows what else). Couldn't find a Sun bug#.

为什么它不起作用:它很好用!
除非它没有。当我有一个程序使用它在后台打开并运行时,我打开了一个 DirectX 游戏(辐射:新维加斯)。Java 程序立即终止,没有堆栈跟踪。总是可重现的(对我来说,在那场比赛中,谁知道还有什么)。找不到 Sun 错误#。

So is there any method to find a user's Documents folder, on Windows, from Java, that doesn't have known problems?

那么有没有什么方法可以在 Windows 上从 Java 中找到没有已知问题的用户的 Documents 文件夹?

(This is a nice big question.)

(这是一个很好的大问题。)

*(The key is "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders")

*(关键是“HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders”)

回答by marcprux

There's no pure java way to do it, but you could use the JNA wrapper over JNI to do it without having to write any native code yourself. There's a good example of how to get the Documents folder on Windows halfway down the responses at:

没有纯 Java 方法可以做到这一点,但您可以在 JNI 上使用 JNA 包装器来做到这一点,而无需自己编写任何本机代码。有一个很好的例子说明如何在 Windows 上获取 Documents 文件夹中的响应:

What is the best way to find the users home directory in Java?

在 Java 中查找用户主目录的最佳方法是什么?

回答by Thishin

A time consuming, but reliable way of finding the 'Documents' folder of a windows user: Make your java app execute a bat script that uses Reg.exe (a windows system file) to find the value of the reg key which has the path in it. Then use a pipeline in the same bat file to send that data to the 'findstr' function which windows command prompt has. Use another pipeline to output the returned value to a text file. Then, simply make your java app read that text file, and delete it once its done :) Worked well enough for me.

查找 Windows 用户的“文档”文件夹的一种耗时但可靠的方法:让您的 Java 应用程序执行一个 bat 脚本,该脚本使用 Reg.exe(Windows 系统文件)来查找具有路径的 reg 键的值在里面。然后在同一个 bat 文件中使用管道将该数据发送到 windows 命令提示符具有的“findstr”函数。使用另一个管道将返回值输出到文本文件。然后,只需让您的 Java 应用程序读取该文本文件,并在完成后将其删除 :) 对我来说效果很好。

Code for the bat file:

bat文件的代码:

@ echo off

Title Find Documents Folder

Reg Query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" |findstr "Personal">>DocPath.dat

exit

回答by Evan Mulawski

There is a custom Java API that someone built (their website no longer works), but there code remains on Google Code:

有人构建了一个自定义 Java API(他们的网站不再工作),但代码仍保留在 Google Code 上:

http://winfoldersjava.googlecode.com/files/WinFoldersJava_1.1.zip

http://winfoldersjava.googlecode.com/files/WinFoldersJava_1.1.zip

There are two DLL's that need to be referenced, one for each architecture(x86 and x64).

有两个 DLL 需要引用,每个体系结构(x86 和 x64)一个。

回答by Xazax

user.home is not "my documents", but users home folder, like on Unix ~/. To get to "My documents" you can use System.getProperty("user.home")+"\Documents"; irrespective of the language system. Try it.

user.home 不是“我的文档”,而是用户主文件夹,如 Unix ~/. 要访问“我的文档”,您可以使用 System.getProperty("user.home")+"\Documents"; 与语言系统无关。尝试一下。