小编典典
您可以使用注册表查询来获取它,而无需JNA或管理员权限。
Runtime.getRuntime().exec("reg query \"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell
Folders\" /v personal");
显然,这将在Windows以外的任何其他版本上失败,并且我不确定这是否适用于Windows XP。
编辑:将其放入代码的工作序列中:
String myDocuments = null;
try {
Process p = Runtime.getRuntime().exec("reg query \"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\" /v personal");
p.waitFor();
InputStream in = p.getInputStream();
byte[] b = new byte[in.available()];
in.read(b);
in.close();
myDocuments = new String(b);
myDocuments = myDocuments.split("\\s\\s+")[4];
} catch(Throwable t) {
t.printStackTrace();
}
System.out.println(myDocuments);
请注意,这将锁定该过程,直到完成“ reg查询”为止,这可能会导致麻烦,具体取决于您在做什么。
2020-09-26