A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: AIR FileSystem - Full Local Access

Threaded View

  1. #1
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,377

    AIR FileSystem - Full Local Access

    Here's a way to read/write anywhere on any local or mapped hard drive with AIR. (Coded for non-Flex):

    Code:
    import flash.filesystem.*;
    
    var _drive:File = null;
    var _stream:FileStream = null;
    var _drives:Array = null;
    var _localFile:File = null;
    
    var _fileName:String = "C:\\testWrite.txt";
    
    function doIt(fileName:String) {
         _drives = File.getRootDirectories();
         for (var i:int = 0; i< _drives.length; i++) {
              if (_drives[i].name == fileName.substring(0, 2)) {
                   _drive = _drives[i];
              }
         }
         if (_drive != null) {
              _stream = new FileStream();
              _localFile = _drive.resolvePath(fileName);
              _stream.open(_localFile, FileMode.WRITE);
              _stream.writeUTFBytes("Hello world!");
              _stream.close();
         }
    }
    
    doIt(_fileName);
    The idea here is simple. File.GetRootDirectories() returns an array of File objects (the root of each drive, mapped or otherwise). You then compare the name of the drive (C:, D:, E:, etc) to the first two characters of the fileName variable. If you find a match, you use that File object (_drive) to resolve the full path of the file you want to open. Hope it helps.

    Please don't comment about the issue of security here. This only works in AIR so you should already be aware of the security risk involved (we've all known it was possible to get around the limitation, I'm just showing how it's done). What you do with it is your responsibility.
    Last edited by ImprisonedPride; 10-13-2009 at 01:55 PM.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center