## Thursday, March 10, 2011

### Introduction to Adobe AIR: Part 9

Today we will learn about absolute paths.

If needed, you can refer to files and directories by specifying absolute paths to them. You can do that when defining a file object:

import flash.filesystem.File;

var exampleDirectory:File = new File("C:/Documents and Settings/Usernamehere/My Documents/Air Examples/");


As you can see, I have used the / slash separator in the example above. Unlike the resolvePath() method, the File constructor allows us to use both / and \ separators. However, if you use \, you'll need to double them (because the \ symbol is reserved and to pass it as a text value you need to neutralize it first).

var exampleDirectory:File = new File("C:\\Documents and Settings\\Usernamehere\\My Documents\\Air Examples\\");


If you need to return an absolute path to a file, use the nativePath property of the File class. For example, to see the absolute path to the user's documents directory, use it like this:

var exampleDocumentsPath:String = File.documentsDirectory.nativePath;
trace(exampleDocumentsPath);


The result will be something like this (I'm running Windows):

C:\Users\7 User\Documents


Here's another example of using nativePath, this time on a File class that we have created using resolvePath first:

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


And the output is:

C:\Users\7 User\Documents\Air Examples\text.txt


Related:

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 8