How can I get list of all drives but also get the corresponding drive type (removable,local disk, or cd-rom,dvd-rom… etc)?

前端 未结 7 1036
猫巷女王i
猫巷女王i 2020-12-01 14:42

How can I get list all drives but also get the corresponding drive type (removable,local disk, or cd-rom,dvd-rom... etc)?

相关标签:
7条回答
  • 2020-12-01 15:07

    If you use JACOB, you can list all drives along with appropriate types:

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

    Example output:

    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}
    

    To use JACOB, add the JAR and DLL's from the download as libraries in your project. This solution is Windows only of course.

    0 讨论(0)
提交回复
热议问题