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

好久不见. 提交于 2019-12-17 06:53:31

问题


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

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.

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.

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.


回答1:


FileSystemView filesys = FileSystemView.getFileSystemView();

File[] roots = filesys.getRoots();

filesys.getHomeDirectory()



回答2:


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


来源:https://stackoverflow.com/questions/570401/in-java-under-windows-how-do-i-find-a-redirected-desktop-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!