« ComboBox With Tooltip
» HTML Tags And Attributes Supported by Flash Player

ActionScript, Air, How to, Uncategorized

AIR applications File and Directory

08.05.08 | 3 Comments

In the past days I had to write a small app in AIR and one of the problems I came across was the saving of preferences locally. I needed to be able to read/write a file on the local file system. After some digging I found the answer and decided to make a small tutorial on how AIR works with files/directories.

So this is the first part of a series of articles on how to use the filesystem from an AIR application. The last article in the series will also include some workin examples. (Parts of the information presented here is obtained from the ADOBE library).

Directories info

File.userDirectory

Description User’s home directory.
Windows The home directory is the parent of the “My Documents” directory (for example, “C:\Documents and Settings\userName\My Documents”).
Mac OS The home directory is the Users/userName directory.
Notes

File.documentsDirectory

Description User’s documents directory.
Windows This is typically the “My Documents” directory (for example, “C:\Documents and Settings\userName\My Documents”).
Mac OS It is the Users/userName/Documents directory.
Notes

File.desktopDirectory

Description User’s desktop
Windows
Mac OS
Notes

File.applicationStorageDirectory

Description Application storage directory. For every AIR application, there is a unique associated path that defines the application storage directory. This directory is unique to each application and user.
Windows In the documents and Settings directory, in user name/Application Data/applicationID.publisherID/Local Store/ (C:\Documents and Settings\babbage\Application Data\com.example.TestApp. 02D88EEED35F84C264A183921344EEA353A629FD.1\ Local Store)
Mac OS In /Users/user name/Library/Preferences/applicationID.publisherID/Local Store/ (/Users/babbage/Library/Preferences/ com.example.TestApp.02D88EEED35F84C264A183921344EEA353A629FD.1 /Local Store)
Notes The URL (and url property) for a File object created with File.applicationStorageDirectory uses the app-storage URL scheme.

var dir = File.applicationStorageDirectory;
dir = dir.resolvePath("prefs.xml");
trace(dir.url); // app-storage:/preferences

File.applicationDirectory

Description The directory in which the application was installed, known as the application directory.
Windows
Mac OS
Notes The URL (and url property) for a File object created with File.applicationDirectory uses the app URL scheme.

var dir = File.applicationDirectory;
dir = dir.resolvePath("prefs.xml");
trace(dir.url); // app:/preferences

File.getRootDirectories()

Description Lists all root volumes
Windows All volumes such as C: and mounted volumes
Mac OS Always returns the unique root directory for the machine (the “/” directory)
Notes

File()

Description Used to point to an explicit directory
Windows
Mac OS
Notes
var file = new File();
file.nativePath = "C:\\AIR Test\\";

or by using the url property

var urlStr = "file:///C:/AIR Test/";
var file = new File()
file.url = urlStr;

currentDirectory

Description The directory location from which an application is invoked can be found out by checking the currentDirectory property of the InvokeEvent object dispatched when the application is invoked.
Windows
Mac OS
Notes

Working with directories

Creating directories

The File.createDirectory() method lets you create a directory. For example, the following code creates a directory named AIR Test as a subdirectory of the user’s home directory:

var dir = File.userDirectory.resolvePath("AIR Test");
dir.createDirectory();

If the directory exists, the createDirectory() method does nothing.

Also, in some modes, a FileStream object creates directories when opening files. Missing directories are created when you instantiate a FileStream instance with the fileMode parameter of the FileStream() constructor set to FileMode.APPEND or FileMode.WRITE.

Creating a temporary directory

The File class includes a createTempDirectory() method, which creates a directory in the temporary directory folder for the System, as in the following example:

var temp = File.createTempDirectory();

The createTempDirectory() method automatically creates a unique temporary directory (saving you the work of determining a new unique location).

You may use a temporary directory to temporarily store temporary files used for a session of the application. Note that there is a createTempFile() method for creating new, unique temporary files in the System temporary directory.

You may want to delete the temporary directory before closing the application, as it is not automatically deleted.

Enumerating directories

You can use the getDirectoryListing() method or the getDirectoryListingAsync() method of a File object to get an array of File objects pointing to files and subfolders in a directory.

For example, the following code lists the contents of the user’s documents directory (without examining subdirectories):

var directory = air.File.documentsDirectory;
var contents = directory.getDirectoryListing(); 
for (i = 0; i < contents.length; i++) 
{
    alert(contents[i].name, contents[i].size); 
}

When using the asynchronous version of the method, the directoryListing event object has a files property that is the array of File objects pertaining to the directories:

var directory = air.File.documentsDirectory;
directory.getDirectoryListingAsync();
directory.addEventListener(air.FileListEvent.DIRECTORY_LISTING, dirListHandler);
 
function dirListHandler(event)
{
    var contents = event.files;
    for (i = 0; i < contents.length; i++) 
    {
        alert(contents[i].name, contents[i].size); 
    }
}

Copying and moving directories

You can copy or move a directory, using the same methods as you would to copy or move a file. For example, the following code copies a directory synchronously:

var sourceDir = air.File.documentsDirectory.resolvePath("AIR Test");
var resultDir = air.File.documentsDirectory.resolvePath("AIR Test Copy");
sourceDir.copyTo(resultDir);

When you specify true for the overwrite parameter of the copyTo() method, all files and folders in an existing target directory are deleted and replaced with the files and folders in the source directory (even if the target file does not exist in the source directory).

The directory that you specify as the newLocation parameter of the copyTo() method specifies the path to the resulting directory; it does not specify the parent directory that will contain the resulting directory.

Deleting directory contents

The File class includes a deleteDirectory() method and a deleteDirectoryAsync() method. These methods delete directories, the first working synchronously, the second working asynchronously (see AIR file basics). Both methods include a deleteDirectoryContents parameter (which takes a Boolean value); when this parameter is set to true (the default value is false) the call to the method deletes non-empty directories; otherwise, only empty directories are deleted.

For example, the following code synchronously deletes the AIR Test subdirectory of the user’s documents directory:

var directory = air.File.documentsDirectory.resolvePath("AIR Test");
directory.deleteDirectory(true);

The following code asynchronously deletes the AIR Test subdirectory of the user’s documents directory:

var directory = air.File.documentsDirectory.resolvePath("AIR Test");
directory.addEventListener(air.Event.COMPLETE, completeHandler)
directory.deleteDirectoryAsync(true);
 
function completeHandler(event) {
    alert("Deleted.")
}

Also included are the moveToTrash() and moveToTrashAsync() methods, which you can use to move a directory to the System trash.

Popularity: 66%

Share and Enjoy:
  • Technorati
  • StumbleUpon
  • del.icio.us
  • NewsVine
  • Reddit
  • Digg
  • Furl
  • co.mments
  • blogmarks
  • Slashdot
  • DZone
  • Taggly
  • YahooMyWeb
  • connotea
  • Webride




Tags: , , , ,

This post was written by Virgil Cristea

Views: 1462

related

popular

3 Comments

have your say

Add your comment below, or trackback from your own site. Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

:

:


« ComboBox With Tooltip
» HTML Tags And Attributes Supported by Flash Player