Here's a way to read/write anywhere on any local or mapped hard drive with AIR. (Coded for non-Flex):
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.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);
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.




Reply With Quote