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 1083
猫巷女王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 14:53

    The correct way is how Luciano answered with isRemovable property, but I recently had on win 10 that if I started from USB, then USBs were no longer found in that method.

    So, I used, for Windows only, the following wmic calls:

    ArrayList rootDirs = new ArrayList();
    if( isWin )
    {
    
    
      if( i_dolog )
        LOG.info( "Windows found as OS." );
      ArrayList partids = new ArrayList();
    
      String results0 = execute( "wmic diskdrive where interfacetype='USB' assoc /assocclass:Win32_DiskDriveToDiskPartition" );
      String lines[] = results0.split( "\r\r\n" );
      for( String line : lines )
      {
        String test = "Win32_DiskPartition.DeviceID=";
        int index = line.indexOf( test );
        if( index >= 0 )
        {
          String partid = line.substring( index + test.length() + 1 );
          partid = partid.substring( 0, partid.indexOf( '"' ) );
          partids.add( partid );
        }
      }
      for( String partition : partids )
      {
        String results2 = execute( "wmic partition where (DeviceID='" + partition + "') assoc /assocclass:Win32_LogicalDiskToPartition" );
        String lines2[] = results2.split( "\r\r\n" );
        for( String line : lines2 )
        {
          String test = "Win32_LogicalDisk.DeviceID=";
          int index = line.indexOf( test );
          if( index >= 0 )
          {
            String partid = line.substring( index + test.length() + 1 );
            partid = partid.substring( 0, partid.indexOf( '"' ) );
            rootDirs.add( new File( partid + "\\" ) );
          }
        }
      }
    }
    

提交回复
热议问题