【发布时间】:2009-07-03 19:52:17
【问题描述】:
我认为这仅适用于英语 Windows 安装:
System.getProperty("user.home") + "/Desktop";
我怎样才能使它适用于非英语 Windows?
【问题讨论】:
我认为这仅适用于英语 Windows 安装:
System.getProperty("user.home") + "/Desktop";
我怎样才能使它适用于非英语 Windows?
【问题讨论】:
我使用的是法语版本的 Windows,并附有说明:
System.getProperty("user.home") + "/Desktop";
对我来说很好。
【讨论】:
我认为这是同一个问题...但我不确定!:
In java under Windows, how do I find a redirected Desktop folder?
阅读它,我希望该解决方案返回 user.home,但显然不是,并且答案 cmets 中的链接支持这一点。自己没试过。
我想通过使用JFileChooser 解决方案将需要一个非无头 JVM,但您可能正在运行其中一个。
【讨论】:
File home = FileSystemView.getFileSystemView().getHomeDirectory(); 然后如果你需要它作为字符串 home.getAbsolutePath();
javax.swing.filechooser.FileSystemView.getFileSystemView().getHomeDirectory()
【讨论】:
user.home
这仅适用于 Windows。启动 REG.EXE 并捕获其输出:
import java.io.*;
public class WindowsUtils {
private static final String REGQUERY_UTIL = "reg query ";
private static final String REGSTR_TOKEN = "REG_SZ";
private static final String DESKTOP_FOLDER_CMD = REGQUERY_UTIL
+ "\"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\"
+ "Explorer\\Shell Folders\" /v DESKTOP";
private WindowsUtils() {}
public static String getCurrentUserDesktopPath() {
try {
Process process = Runtime.getRuntime().exec(DESKTOP_FOLDER_CMD);
StreamReader reader = new StreamReader(process.getInputStream());
reader.start();
process.waitFor();
reader.join();
String result = reader.getResult();
int p = result.indexOf(REGSTR_TOKEN);
if (p == -1) return null;
return result.substring(p + REGSTR_TOKEN.length()).trim();
}
catch (Exception e) {
return null;
}
}
/**
* TEST
*/
public static void main(String[] args) {
System.out.println("Desktop directory : "
+ getCurrentUserDesktopPath());
}
static class StreamReader extends Thread {
private InputStream is;
private StringWriter sw;
StreamReader(InputStream is) {
this.is = is;
sw = new StringWriter();
}
public void run() {
try {
int c;
while ((c = is.read()) != -1)
sw.write(c);
}
catch (IOException e) { ; }
}
String getResult() {
return sw.toString();
}
}
}
或者您可以使用 JNA (complete example here)
Shell32.INSTANCE.SHGetFolderPath(null,
ShlObj.CSIDL_DESKTOPDIRECTORY, null, ShlObj.SHGFP_TYPE_CURRENT,
pszPath);
【讨论】:
好像没那么简单……
但是你可以尝试找一个浏览器浏览一些开源项目的代码,例如在Koders。我想所有的解决方案都归结为检查 Windows 注册表中的 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Desktop 路径。并且可能是特定于 Windows 的。
如果您需要更通用的解决方案,我会尝试找到一个您知道在不同平台上正常工作的开源应用程序,并将一些图标放在用户的桌面上。
【讨论】:
你只是缺少"C:\\Users\\":
String userDefPath = "C:\\Users\\" + System.getProperty("user.name") + "\\Desktop";
【讨论】:
public class Sample {
public static void main(String[] args) {
String desktopPath =System.getProperty("user.home") + "\\"+"Desktop";
String s = "\"" + desktopPath.replace("\\","\\\\") + "\\\\" +"satis" + "\"";
System.out.print(s);
File f = new File(s);
boolean mkdir = f.mkdir();
System.out.println(mkdir);
}
}
【讨论】:
有两件事。
\ 而不是 /。\\) 来分隔文件夹名称。【讨论】:
最简单的解决方案是找出机器名称,因为这个名称只是桌面文件夹路径中的变量。所以如果你能找到这个,你就找到了桌面的路径。以下代码应该可以解决问题 - 它对我有用:)
String machine_name = InetAddress.getLocalHost().getHostName();
String path_to_desktop = "C:/Documents and Settings/"+machine_name+"/Desktop/";
【讨论】: