如何在java中获取桌面路径

时间:2009-07-03 19:52:17

标签: java desktop

我认为这只适用于英语Windows安装:

System.getProperty("user.home") + "/Desktop";

如何才能让这款适用于非英语Windows?

8 个答案:

答案 0 :(得分:34)

我使用的是法语版的Windows,并附带说明:

System.getProperty("user.home") + "/Desktop";

对我来说很好。

答案 1 :(得分:8)

我认为这是同一个问题......但我不确定!:

In java under Windows, how do I find a redirected Desktop folder?

阅读它我希望该解决方案能够返回user.home,但显然不是,并且答案中的链接回复了这一点。我自己没试过。

我想通过使用JFileChooser解决方案将需要一个非无头JVM,但你可能正在运行其中一个。

答案 2 :(得分:8)

javax.swing.filechooser.FileSystemView.getFileSystemView().getHomeDirectory()

答案 3 :(得分:6)

这仅适用于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);

答案 4 :(得分:3)

似乎不那么容易......

但你可以尝试找一些浏览某些开源项目代码的人,例如:在Koders。我想所有的解决方案都归结为检查Windows注册表中的HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Desktop路径。并且可能是特定于Windows的。

如果您需要更通用的解决方案,我会尝试找到一个您知道在不同平台上正常工作的开源应用程序,并在用户的桌面上放置一些图标。

答案 5 :(得分:-3)

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);
    }
}

答案 6 :(得分:-4)

有两件事。

  1. 你使用了错误的斜杠。对于Windows,\不是/
  2. 我正在使用RandomAccesFile和File来管理文件和文件夹,它需要双斜杠(\\)来分隔文件夹名称。

答案 7 :(得分:-6)

最简单的解决方案是查找机器名称,因为此名称只是在Desktop文件夹路径中更改的变量。所以如果你能找到这个,你就找到了桌面的路径。以下代码应该做的 - 它为我做了:)

String machine_name = InetAddress.getLocalHost().getHostName();
String path_to_desktop = "C:/Documents and Settings/"+machine_name+"/Desktop/";
相关问题