在Windows下的Java中,如何找到重定向的桌面文件夹? [英] In java under Windows, how do I find a redirected Desktop folder?

查看:196
本文介绍了在Windows下的Java中,如何找到重定向的桌面文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道使用C#等.NET语言,可以做一些类似于

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

Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)

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

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.

这个应用程序需要写入应用程序数据树以及可选的桌面。我假定%APPDATA%始终正确填充,即使在重定向文件夹时,也可以找到应用程序数据树。所以我开放的问题是如何可靠地找到桌面文件夹。

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.

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

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.

推荐答案

FileSystemView filesys = FileSystemView.getFileSystemView();

File[] roots = filesys.getRoots();

filesys.getHomeDirectory()

这篇关于在Windows下的Java中,如何找到重定向的桌面文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆