如何在java中获取桌面路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1080634/
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-08-11 23:18:28  来源:igfitidea点击:

How to get the Desktop path in java

javadesktop

提问by jumar

I think this will work only on an English language Windows installation:

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

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

How can I make this work for non English Windows?

我怎样才能使这项工作适用于非英语 Windows?

回答by Grzegorz Oledzki

Seems not that easy...

好像没那么容易...

But you could try to find an anwser browsing the code of some open-source projects, e.g. on Koders. I guess all the solutions boil down to checking the HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Desktoppath in the Windows registry. And probably are Windows-specific.

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

If you need a more general solution I would try to find an open-source application you know is working properly on different platforms and puts some icons on the user's Desktop.

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

回答by Dan Gravell

I think this is the same question... but I'm not sure!:

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

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

在Windows下的java中,如何找到重定向的桌面文件夹?

Reading it I would expect that solution to return the user.home, but apparently not, and the link in the answer comments back that up. Haven't tried it myself.

阅读它,我希望该解决方案返回 user.home,但显然不是,并且答案中的链接对此进行了评论。自己没试过。

I guess by using JFileChooserthe solution will require a non-headless JVM, but you are probably running one of them.

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

回答by RealHowTo

This is for Windows only. Launch REG.EXE and capture its output :

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

or you can use JNA (complete example here)

或者您可以使用 JNA(此处为完整示例

   Shell32.INSTANCE.SHGetFolderPath(null,
      ShlObj.CSIDL_DESKTOPDIRECTORY, null, ShlObj.SHGFP_TYPE_CURRENT,
      pszPath);

回答by Princely Royan

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

回答by Amlaa

I use a french version of Windows and with it the instruction:

我使用法语版本的 Windows 并使用它的说明:

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

works fine for me.

对我来说很好用。

回答by j-cup

Simplest solution is to find out machine name, since this name is only variable changing in path to Desktop folder. So if you can find this, you have found path to Desktop. Following code should do the trick - it did for me :)

最简单的解决方案是找出机器名称,因为该名称只是桌面文件夹路径中的变量。所以如果你能找到这个,你就找到了桌面的路径。以下代码应该可以解决问题 - 它对我有用:)

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

回答by augugusto

there are 2 things.

有两件事。

  1. you are using the wrong slash. for windows it's \not /.
  2. i'm using RandomAccesFile and File to manage fles and folders, and it requires double slash ( \\) to separate the folders name.
  1. 你使用了错误的斜线。对于 Windows,它\不是/
  2. 我正在使用 RandomAccesFile 和 File 来管理文件和文件夹,它需要双斜线 ( \\) 来分隔文件夹名称。

回答by mauretto

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