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

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

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

javawindowsredirectfinddesktop

提问by Eddie

I know using .NET languages such as C#, one can do something like

我知道使用 C# 等 .NET 语言,可以做类似的事情

Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)

to find the redirected location of the Desktop. However, under Java, I cannot think of a good way to do this. What is the most appropriate way to find a redirected user Desktop directory from Java, without using JNI? The specific purpose here is for the purposes of managing a desktop shortcut, if the user wants one, for a Java Web Start application.

找到桌面的重定向位置。但是,在 Java 下,我想不出一个好的方法来做到这一点。在不使用 JNI 的情况下,从 Java 查找重定向用户桌面目录的最合适方法是什么?此处的特定目的是为了管理 Java Web Start 应用程序的桌面快捷方式(如果用户需要)。

This application needs to write to the "Application Data" tree as well as optionally to the Desktop. I am making the assumption that %APPDATA%is always correctly populated, even when folders are redirected, for finding the "Application Data" tree. So my open question is how to reliably find the Desktop folder.

此应用程序需要写入“应用程序数据”树以及可选的桌面。我假设%APPDATA%总是正确填充,即使文件夹被重定向,用于查找“应用程序数据”树。所以我的悬而未决的问题是如何可靠地找到桌面文件夹。

NOTE: I believe that the Java system property ${user.home}actually (and erroneously) locates the user's Desktop directory via registry keys and then tries to navigate up one directory to find the "home" directory. This works fine when no directories are redirected, and otherwise may or may not return something useful.

注意:我相信 Java 系统属性${user.home}实际上(并且错误地)通过注册表项定位用户的桌面目录,然后尝试向上导航一个目录以找到“主”目录。当没有目录被重定向时,这可以正常工作,否则可能会或可能不会返回有用的东西。

回答by Russ Bradberry

FileSystemView filesys = FileSystemView.getFileSystemView();

File[] roots = filesys.getRoots();

filesys.getHomeDirectory()

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