AIR API Tip: Don’t write to File.applicationDirectory

As a number of AIR developers have discovered, it’s hard—but not impossible—to write files in the application’s install directory. That’s the same location given by File.applicationDirectory.

Granted, it’s often a tempting thing to do. For example, many applications include a database or configuration file with some pre-filled data. Once installed, they often want to update that file, say with user-specific data or preferences.

The first problem with this is that it’s not reliable. On some operating systems—Vista, for example—the installation directory is protected by the operating system. Even if AIR lets you write to this location Vista won’t. So if you want to write portable applications, don’t do this.

The second problem with this is that it’s not safe. Any code written into this directory runs with application privilege, which can compromise your application, which can compromise the user’s machine. That’s why Vista protects these directories.

The third problem is that it invalidates your application’s signature. Among other things, that means you won’t have access to the encrypted local store any more.

It’s also unnecessary. A safe and allowable alternative is to write somewhere into the user’s directory. If you still want a starter file, it’s easy to copy one from your install directory to the per-user location. Note that this also avoid problems if there are multiple users on the same machine.

In order to help developers avoid this pattern, AIR will prevent write access to this directory in most cases, and even when the underlying operating system allows it. There are currently ways around this, as it’s advisory—not mandatory. However, the ability to go around this restriction is often misinterpreted as a defect, and we may close this off in a future release. You’ve been warned.

5 Responses to AIR API Tip: Don’t write to File.applicationDirectory

  1. lrpqx ebulc says:

    kubds rqhpzgsm fihrxgm awyui jmsq vgplstim uedizngc

  2. Erica Norton says:

    Note too that any files written to the app directory will be missed during an uninstall on Windows platforms (that allow writing to that diretory).

  3. Tom Esposito says:

    This just bit me. I wanted to put a logging properties file in the application directory. Then if the user had issues, part of our troubleshooting protocol would be to go to this directory and change the log level. I guess I’ll have to put that in the user’s directory instead.Tom

  4. jr says:

    I’m developing an air applictation and I’ve come across an issue I can’t seem figure out. The application has a user agreement on first launch after the user agrees, I am writing a file to the applicationStorageDirectory, which tells the app that the user has agreed. This way the agreement only displays on first run. But if the user uninstalls and reinstalls the app, I need to display the agreement window again. Uninstalling the application doesn’t seem to delete anything in the applicationStorageDirectory.Any ideas?

  5. Divakarla Srinivas says:

    to come to out of the problems with the unistallation, add a 0KB file in your AIR settings, read it if there is nothing in that then assume that the user is running it for the very first time, but allways make sure that when u bundle the file it must be a 0KB