Wednesday, March 9, 2011

Introduction to Adobe AIR: Part 8

Today we will learn about referring to files using relative paths.

As I have mentioned multiple times in the previous tutorials, AIR is cross-platform. That means it is made to work on all platforms the same and AIR is supposed to take care of all the cross-OS business while developer writes one code that works everywhere. However, as you might already know, the structure of directory and file paths in different OS are different. For example, we want to access a directory called "Air Examples" which is located inside the user's Documents directory.

In Windows the path to that folder would be something like this:

C:\Documents and Settings\Usernamehere\My Documents\Air Examples\

The path to the same directory in Mac OS would be different:

/Users/Usernamehere/Documents/Air Examples

Even if we find out the OS our user is using and construct the correct path, we still don't know the user name (Usernamehere).

This is why sometimes it is best to use relative paths. For example, to create a File object with path to the same Air Examples folder, we can do it like this:

import flash.filesystem.File;

var exampleDirectory:File = File.documentsDirectory.resolvePath("Air Examples");

The resolvePath() method allows us to successfully get to the files we want without any unnecessary additional pieces of code. If we want to access sub-directories and files, use the / (not \) symbol:

var exampleText:File = File.documentsDirectory.resolvePath("Air Examples/text.txt");

We can access the parent folder this way:

var exampleParentDirectory:File = File.documentsDirectory.resolvePath("..");

We can find out the relative path between two directories or files too. For example, the path from documents directory to users directory:

var examplePath:String = File.userDirectory.getRelativePath(File.documentsDirectory);

That's all for now.

Thanks for reading!


Introduction to Adobe AIR: Part 1
Introduction to Adobe AIR: Part 2
Introduction to Adobe AIR: Part 3
Introduction to Adobe AIR: Part 4
Introduction to Adobe AIR: Part 5
Introduction to Adobe AIR: Part 6
Introduction to Adobe AIR: Part 7
Introduction to Adobe AIR: Part 9

1 comment:

digital certificate said...

As you said that Adobe air is made to work on all platforms.That is a big feature in itself.Thanks for giving code to access the file structure so that code work properly on all OS

Post a Comment