如何获得所有驱动器的列表,还可以获取相应的驱动器类型(可移动,本地磁盘或光盘,DVD-ROM等)?如何获得所有驱动器的列表,但也可以获取相应的驱动器类型(可移动,本地磁盘或光盘,DVD-ROM等)?
回答
没有定义你要问什么。
我有一个拇指驱动器,插入时显示为CD-ROM。在CD-ROM上运行程序后,它会附加另一个看起来像是硬盘的分区。
我有可移动的CD-ROM驱动器。我的电脑外部还有“内部”eSATA硬盘驱动器。
您必须非常努力地获得驱动器“类型”的绑定定义。你为什么不告诉我们你想做什么,而不是询问你想要做什么的特定方式?
谢谢你的回应!我假设所有的CD/DVD驱动器都是“可移动的”,我也想知道通过USB连接的某些驱动器也假定它是“可移动的”。 Subst(ed)文件夹也是“可移动的”。所以,简单地说,我想知道用户是否可以轻松拔掉某些驱动器。 – xolmc 2010-08-22 19:28:44
假设它是windows,使用File.listRoots()
来获得所有的根。 然后用FileSystemView
来检查它是软盘还是硬盘。除此之外,我不知道。
有了这个代码,你可以得到所有驱动器和它的类型描述
File[] paths;
FileSystemView fsv = FileSystemView.getFileSystemView();
// returns pathnames for files and directory
paths = File.listRoots();
// for each pathname in pathname array
for(File path:paths)
{
// prints file and directory paths
System.out.println("Drive Name: "+path);
System.out.println("Description: "+fsv.getSystemTypeDescription(path));
}
请注意,描述是依赖于语言的。 – 2015-01-22 16:59:04
好的代码,但你如何以独立于语言的方式获得驱动器类型? – BullyWiiPlaza 2016-01-08 21:24:13
如果使用JACOB
,你可以列出所有驱动器上相应品种一起:
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.EnumVariant;
import com.jacob.com.JacobObject;
import com.jacob.com.Variant;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class DrivesExample
{
public interface HasNativeValue
{
int getNativeValue();
}
public enum DriveTypeEnum implements HasNativeValue
{
Unknown(0),
NoRootDirectory(1),
RemovableDisk(2),
LocalDisk(3),
NetworkDrive(4),
CompactDisc(5),
RAMDisk(6);
public final int nativeValue;
DriveTypeEnum(int nativeValue)
{
this.nativeValue = nativeValue;
}
public int getNativeValue()
{
return nativeValue;
}
}
private static <T extends Enum<T> & HasNativeValue> T fromNative(Class<T> clazz, int value)
{
for (T c : clazz.getEnumConstants())
{
if (c.getNativeValue() == value)
{
return c;
}
}
return null;
}
/**
* The drive information.
*/
public static final class Drive
{
/**
* File system on the logical disk. Example: NTFS. null if not known.
*/
public final String fileSystem;
/**
* Value that corresponds to the type of disk drive this logical disk represents.
*/
public final DriveTypeEnum driveType;
/**
* The Java file, e.g. "C:\". Never null.
*/
public final File file;
public Drive(String fileSystem, DriveTypeEnum driveType, File file)
{
this.fileSystem = fileSystem;
this.driveType = driveType;
this.file = file;
}
@Override
public String toString()
{
return "Drive{" + file + ": " + driveType + ", fileSystem=" + fileSystem + "}";
}
}
/**
* Lists all available Windows drives without actually touching them. This call should not block on cd-roms, floppies, network drives etc.
*
* @return a list of drives, never null, may be empty.
*/
public static List<Drive> getDrives()
{
List<Drive> result = new ArrayList<>();
ActiveXComponent axWMI = new ActiveXComponent("winmgmts://");
try
{
Variant devices = axWMI.invoke("ExecQuery", new Variant("Select DeviceID,DriveType,FileSystem from Win32_LogicalDisk"));
EnumVariant deviceList = new EnumVariant(devices.toDispatch());
while (deviceList.hasMoreElements())
{
Dispatch item = deviceList.nextElement().toDispatch();
String drive = Dispatch.call(item, "DeviceID").toString().toUpperCase();
File file = new File(drive + "/");
DriveTypeEnum driveType = fromNative(DriveTypeEnum.class, Dispatch.call(item, "DriveType").getInt());
String fileSystem = Dispatch.call(item, "FileSystem").toString();
result.add(new Drive(fileSystem, driveType, file));
}
return result;
} finally
{
closeQuietly(axWMI);
}
}
private static void closeQuietly(JacobObject jacobObject)
{
try
{
jacobObject.safeRelease();
} catch (Exception ex)
{
ex.printStackTrace();
}
}
public static void main(String[] arguments)
{
List<Drive> drives = getDrives();
for (Drive drive : drives)
{
System.out.println(drive.toString());
}
}
}
输出示例:
Drive{C:\: LocalDisk, fileSystem=NTFS}
Drive{D:\: LocalDisk, fileSystem=NTFS}
Drive{E:\: RemovableDisk, fileSystem=NTFS}
Drive{F:\: RemovableDisk, fileSystem=FAT32}
Drive{G:\: RemovableDisk, fileSystem=null}
Drive{Y:\: NetworkDrive, fileSystem=NTFS}
要使用JACOB
,请添加JAR
和DLL
作为您项目中的库下载。当然,这个解决方案只有Windows
。
File[] roots = File.listRoots();
for(int i=0;i<roots.length;i++)
System.out.println("Root["+i+"]:" + roots[i]);
这不会打印/包含驱动器类型 – BullyWiiPlaza 2016-01-08 21:23:38
此代码片段适用于Windows。它正确地报告USB笔式驱动器是可移动的,但在我的笔记本电脑上,USB硬盘驱动器被标记为不可移动。无论如何,这是最好的,我发现没有使用本机代码。
for (Path root : FileSystems.getDefault().getRootDirectories()) {
FileStore fileStore = Files.getFileStore(root);
System.out.format("%s\t%s\n", root, fileStore.getAttribute("volume:isRemovable"));
}
- 1. Ruby获取可用磁盘驱动器
- 2. C++ \ Win32的信息有关的所有驱动器:硬盘驱动器,USB驱动器,光盘驱动器,软盘驱动器等
- 3. 如何从驱动器的全名获得驱动器盘符
- 4. 无驱动器盘符的驱动器的Powershell可用磁盘空间
- 5. WinForms:驱动器中没有磁盘。请将磁盘插入驱动器
- 6. 如何获得可移动磁盘在C#的列表?
- 7. 获取硬盘的驱动器号
- 8. 错误地获取磁盘驱动器信息
- 9. 获取驱动器盘符在Windows
- 10. 如何从本地磁盘驱动器读取文件,如D://或E://或C://
- 11. 确定驱动器中有光盘
- 12. 通过WMI查询获取挂载点的相应物理磁盘驱动器?
- 13. 硬盘或硬盘驱动器
- 14. 获得驱动器的所有文件
- 15. Eclipse Galileo中缺少磁盘驱动器
- 16. 获取包括共享RDP驱动器的驱动器列表
- 17. 检测CMD中的可移动驱动器盘符
- 18. 德尔福 - 如何获得Windows驱动器的总磁盘空间?
- 19. PowerShell的获得驱动器列表
- 20. 硬盘驱动器磁盘读取缓存
- 21. 驱动器盘查看器
- 22. 禁用使用光盘驱动器(VB.NET)
- 23. 如何获得跨平台C++或C的U盘的挂载点/磁盘驱动器?
- 24. 如何获取DevicePath的驱动器盘符
- 25. 添加新的磁盘datanode与更大的硬盘驱动器
- 26. 如何使用python或WMI设置RAM磁盘驱动器?
- 27. 如何从.NET Framework中的所有本地驱动器识别外部硬盘驱动器?
- 28. 如何从Windows 7的驱动器号中获取可移动设备的物理驱动器号?
- 29. C# - 从硬盘驱动器
- 30. 删除驱动器盘符
这是在Windows环境中? – Woot4Moo 2010-08-22 15:49:36