如何从 JFileChooser 检索 UNC 路径而不是映射的驱动器路径
只是想知道是否有办法从使用 JFileChooser 选择的文件返回 UNC 路径。我要选择的文件将驻留在具有 UNC 路径的映射驱动器上。现在,我似乎只能拉回映射驱动器的驱动器号。
Just wondering if there is a way to return back the UNC path from a file chosen with JFileChooser. The file that I would be selecting would reside on a mapped drive that has a UNC path. Right now, I can only seem to pull back the drive letter of a mapped drive.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从 https://stackoverflow.com/users/715934/tasoocoo
我最终找到了一个解决方案执行 NET USE 命令:
From https://stackoverflow.com/users/715934/tasoocoo
I ended up finding a solution that executes the NET USE command:
正如我对 Gerry 的回答的评论,
ShellFolder.getDisplayName
是不可靠的,因为用户可以将显示名称更改为他们想要的任何名称。然而,UNC 路径似乎可以通过
sun.awt.shell.ShellFolder
获得。这当然是一个“内部专有 API”,因此不能保证这将在未来版本的 java 中继续工作,但在 Windows 7 中针对 java 1.8.0_31 进行测试时,我看到一个名为Attributes 的
对于网络驱动器,它似乎包含 UNC 路径作为裸ShellFolderColumnInfo
字符串
。例如:如果您在资源管理器中转到“我的电脑”,更改为“详细信息”视图并打开“网络位置”列,它似乎与我们通过
ShellFolder
API 从“属性”获取的内容相匹配。不确定“属性”来自哪里,或者它在非英语语言环境中是否发生变化。As I commented on Gerry's answer,
ShellFolder.getDisplayName
is unreliable because the user can changed the display name to whatever they want.However the UNC path does seem to be available via
sun.awt.shell.ShellFolder
. This is of course an "internal proprietary API" so no guarantee this will continue to work in future versions of java, but testing against java 1.8.0_31 in Windows 7 I see aShellFolderColumnInfo
titledAttributes
which for network drives appears to include the UNC path as a bareString
. eg:If you go to My Computer in explorer, change to Details view and turn on the "Network Location" column, it appears to match what we get from "Attributes" via the
ShellFolder
API. Not sure where "Attributes" comes from or if it changes in non-english locales.JFileChooser
方法getSelectedFile()
返回一个文件
,其中可能包含有用的信息。“对于 Microsoft Windows 平台,...UNC 路径名的前缀是
"\\\\"
;主机名和共享名是名称序列中的前两个名称。”The
JFileChooser
methodgetSelectedFile()
returns aFile
, which may have helpful information."For Microsoft Windows platforms,…the prefix of a UNC pathname is
"\\\\"
; the hostname and the share name are the first two names in the name sequence."如果其他人正在寻找替代(我认为更简单)的解决方案,
您可以使用 ShellFolder.getDisplayName() 查找信息。例如,您可以从此处的字符串解析驱动器的网络位置:
这也可能有用:
If anyone else is looking for an alternate (and I think simpler) solution,
you can find the information using ShellFolder.getDisplayName(). For example, you can parse the network location of the drive from the string here:
This might also be useful: