Detecting Local Drives with Adobe AIR

Detecting a listing for local drives using Adobe AIR is fairly straight forward using the Adobe AIR File function getDirectoryListing for Mac or getRootDirectories for PC. The following code will get you a listing of the local drives for Mac or PC:

import flash.filesystem.File;
import flash.system.Capabilities;

private function getDrives():void
{
     var os:String = Capabilities.os.substr(0, 3).toLowerCase();
     var currentDrives:Array = (os=="mac") ? new File('/Volumes/').getDirectoryListing() : File.getRootDirectories() ;
     for each(var file:File in currentDrives){
         trace ("Drive: " + file.name + " Size: " + file.size);
     }
}

However, what I wanted to do was automatically refresh the the drive listing on a set time interval so I could detect newly added or removed drives. Here is a sample for that:

import flash.events.TimerEvent;
import flash.filesystem.File;
import flash.system.Capabilities;
import flash.utils.Timer;


private function setDriveWatcher():void
{
     var timer:Timer = new Timer( 2000 );
     timer.addEventListener( TimerEvent.TIMER, timerHandler );
     timer.start();
}

private function timerHandler(event : TimerEvent):void
{
     var os:String = Capabilities.os.substr(0, 3).toLowerCase();
     var currentDrives:Array = (os=="mac") ? new File('/Volumes/').getDirectoryListing() : File.getRootDirectories() ;
      for each(var file:File in currentDrives){
         trace ("Drive: " + file.name + " Size: " + file.size);
     }
}

Everything seems to work great on my Mac, but when I tested the application on my PC I heard a strange constant grinding noise coming from the A: drive, you know, that dinosaur floppy drive you probably forgot your PC had. It seems that when AIR gets a listing of all the local drives on the user machine, it pings the A: drive which causes it to make the noise. Remember when you try to access a PC floppy drive when it was empty it makes a lovely grinding noise that sounds like the drive is eating itself.

I suspect this issue arises because I am doing something with the directory detection feature of AIR that was not intended by Adobe, that is to repeatedly get listings of all local drives. However, I am trying to make a hack for a feature that Adobe probably should have included with AIR, automatically detecting drives being added and removed from the users machine. This is the issue that probably needs to be addressed by Adobe. I could not find any way to just skip certain drives, like the A: drive to avoid this issue either.

This issue does not happen on all PC’s with floppy drives, just a handful that I have tested. It seems to be less of a problem when users have an actual floppy in the drive, but its not a total solution. It’s slightly annoying to auto-detect the adding or removing of drives, so I recommend making a manual drive refresh option for users, something they can click one whenever they add or remove an external drive. Now, if I can only get AIR to recognize the type of drives automatically, like the difference between a hard drive, usb drive, and my camera, that would be nice.

Big thanks to my buddy Larry Drolet who originally created this technique.

-Mr

9 Comments

  1. im getting only this in PC
    current drives: [object File]
    how to get drive letter and or drive size will be nice to!

  2. I updated the post to pull out all the File objects in the currentDrives Array and list the file names. Getting the size of the drives is a taller order.

  3. Hello,

    In Air, i try to load a image from a drive (the DVD) but i don’t know how to target the dvd driver with the URLRequest() class ? Is-it possible ?

    David.

  4. I think you’re making a mistake in assuming that there is something wrong with how Adobe “intended” you to use this functionality. Although its probably not common knowledge now since no one uses floppies, but there isn’t a way for Windows OS to know if there is a floppy in the drive _without_ trying to spin the disk up and read from it; this is a failing of the floppy disk technology.

    Basically, the function is doing the only thing it can do to see if your A drive has anything in it, and thats to try spinning it up and reading from it.

    1. Luckily, AIR 2.0 promises to have USB support so there is no longer a need to hit all the drives to see if a new device is connected.

  5. This function is exactly what I was looking for. However, I notice it wouldn’t work right on Linux. I don’t have a linux machine or VM at my disposal here, but I believe the solution make be as simple as modifying the one line to read:

    var currentDrives:Array = (operatingSystem==”win”) ?
    File.getRootDirectories() : new File(‘/Volumes/’).getDirectoryListing();

    I _think_ Linux uses the same /Volumes/ format as OS X (which is FreeBSD-based)

Comments are closed.