Adobe Air and included files

Have you as a developer ever wanted to deploy an Adobe Air application and include files with the installation? Say an XML file for settings and the like? Well, you can include files well enough (to the applicationDirectory), but if you want to write back to that file you’ll get an error. You see, files in the applicationDirectory are read-only.

So what do you do? You’ll need to read that file in the applicationDirectory and then copy it over to the applicationStorageDirectory… or desktopDirectory or even the documentsDirectory. Then you’ll be able to read and write to the file.

The snippet below sets up a reference to a test text document I included in an AIR application. It exists in the protected applicationDirectory. Then the destination (of applicationStorageDirectory) is referenced. If the file doesn’t already exist in the destination location, the one in the applicationDirectory is copied over to the applicationStorageDirectory… and traces “Copy Done” for me.

If you ever wanted to reset the settings file, all you would need to do is to copy over that original included Test.txt file to your applicationStorageDirectory again, over-writing the one that was already there. I didn’t see anything like this in my Google searches, but I did hear of the technique. Here is a little code that shows it in action.

var testFile:File = File.applicationDirectory.resolvePath("Test.txt");
			
//Copy the file over
var destination:File = File.applicationStorageDirectory;
destination = destination.resolvePath("Test.txt");
//Only copy the file from the installation bundle if it doesn't exist in the applicationStorageDirectory
if( !destination.exists ){
	if( testFile.copyTo(destination,true)){
		trace("Copy done");
	}
}

One thing to note – on a Mac, the applicationStorageDirectory exists here: username/Library/Preferences//Local Store/

Of course it exists elsewhere on a PC and Linux, but the applicationStorageDirectory reference takes care of that for you on the different operating systems. You should check your Local Store just to make sure it’s doing what you need it to do.

5 thoughts on “Adobe Air and included files

  1. Thank you for this post, I was googling this issue for hours.

    A minor comment, the location of those files in mac is in {USERNAME}/Library/{YOUR AIR APPLICATION NAME}/Preferences//Local Store/

    cheers

  2. And another small comment, files bundled with your application during ADT packaging are stored WITHIN the application folder (right-click ‘show contents’)…

  3. I am glad that it helped you. I know I was googling at one point and was getting fairly frustrated in not finding a whole lot of information on the topic.

    @PalMediC

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.