从驱动器号获取分区名称,反之亦然 [英] Get partition name from drive letter and vice versa

查看:28
本文介绍了从驱动器号获取分区名称,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理的程序需要访问可移动驱动器.通常这不会有问题,因为挂载点应该保持不变(例如:在 Ubuntu 上,我手机的 SD 卡被挂载在 /media/sebastian/GT-S5830/)但在 Windows 上有驱动器号,可能会有所不同.(同一部手机:曾经E:\,插上后,相机安装在E:,变成F:并停留.)

所以我想通过不保存驱动器号,而是保存分区名称来解决这个问题.

例如:设置时,给出了路径E:\DCIM\Camera\.现在我想做以下事情:

  • 获取挂载在 E:
  • 的分区名称
  • 将给定目录的路径另存为 :\DCIM\Camera\
  • 访问设备时,解析名为partname的分区的盘符
  • 通过连接驱动器号和冒号后的路径部分来构建路径.

如何通过在 Windows 上提供挂载点来获取分区名称,反之亦然?

解决方案

您可能想要探索 FileSystemView 以获取有关文件系统的更多信息.更多分类示例此处.>

出于您的原因,您可能希望获取可移动磁盘的句柄并对信息进行处理:

FileSystemView fsv = FileSystemView.getFileSystemView();File[] 文件 = File.listRoots();File[] 根 = fsv.getRoots();for (int i = 0; i 

输出:

Root: C:\Users\popofibo\Desktop本地磁盘 (C:)恢复 (D:)可移动磁盘 (E:)

如果需要,您可能想使用 JNA 检查 Windows 磁盘的卷信息 - 更多详细信息 此处.

The program I'm working on needs to access removable drives. Normally this wouldn't be a problem, because the mountpoint should stay the same (e.g.: On Ubuntu my phone's SD card gets mounted at /media/sebastian/GT-S5830/) But on Windows there are the drive letters, which can vary. (Same phone: Once E:\, after plugging in while camera was mounted at E:, it became F: and stayed.)

So I want to solve this by not saving the drive letter, but the partition name.

E.g.: When setting up, the path E:\DCIM\Camera\ was given. Now I want to do the following:

  • Get name of the partition mounted at E:
  • Save path to given directory as something like <partname>:\DCIM\Camera\
  • When accessing the device, resolve drive letter of partition named partname
  • Build path by concatenating drive letter and the path-part after the colon.

How can I get the partition name by giving the mountpoint on Windows and vice versa with Java?

解决方案

You might want to explore FileSystemView to get a lot more information about the file system. More assorted examples here.

For your cause you might want to get a handle on the removable disk and do something with the info:

FileSystemView fsv = FileSystemView.getFileSystemView();
        File[] files = File.listRoots();

        File[] roots = fsv.getRoots();
        for (int i = 0; i < roots.length; i++) {
            System.out.println("Root: " + roots[i]);
        }

        for (File fi : files) {
            if (fsv.getSystemTypeDescription(fi).contains("Local Disk")
                    || fsv.getSystemTypeDescription(fi).contains(
                            "Removable Disk")) {
                System.out.println(fsv.getSystemDisplayName(fi));
            }

        }

Output:

Root: C:\Users\popofibo\Desktop
Local Disk (C:)
Recovery (D:)
Removable Disk (E:)

You might want to check the Windows disks' volume information using JNA if need be - more details here.

这篇关于从驱动器号获取分区名称,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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