我们可以通过 System.getProperty("user.home") 读取JAVA系统的user.home 属性的值。
System.getProperty("user.home") 方法先去读取注册表中HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders下的 Desktop 键值做为 user.dir ,再取它的上一级目录做为user.home
打开注册表编辑器,定位到上面的键值,你可以发现 Desktop 的值是%USERPROFILE%\桌面 这种形式
C:\Documents and Settings\Administrator\桌面,
%USERPROFILE% 对应 C:\Documents and Settings\%用户名%。对于 Administrator 用户,这里取得的 Desktop 自然是 C:\Documents and Settings\Administrator\桌面. 那么 user.home 就应该是C:\Documents and Settings\Administrator
示例1:
public class PrintHome{
public static void main(String[] args) {
System.out.println(System.getProperty("user.home") );
}
}
有些电脑的注册表中的Desktop可能变为 %USERPROFILE%\桌面 这种形式。这时我们得到的 user.home 可能会变成 C:\Documents and Settings\Administrator\桌面,这时需要手动修改 Desktop为 %USERPROFILE%\桌面 这种形式