The FileReference class in Flash 10 allows you to save data to a local file in Flash BUT it has some major limitations. While necesary for security in online applications, these limitations make using the FileReference class impractical in standalone applications.

· FileReference.save() can only be called in response to user interaction (mouse or keyboard),
· FileReference.save() displays a dialog and lets the user select the location,
· The location of the saved file is not available to ActionScript, and
· FileReference.save() can only be made as an asynchronous (non-blocking) call.



Enable the FileSys Plugin

The commands we're going to use to save data to a file are all part of the FileSys plugin. Before you can use the FileSys plugin in your Flash application you need to tell SWF Studio to make it part of your EXE. You do that by checking the FileSys checkbox on the Plugins Tab.

Saving Data with SWF Studio

If you have some data to save and you know where you want to save it then SWF Studio makes this a very simple operation. Here's how you would save some text to a file in the root of the C: drive. It doesn't get much easier than this.

ssCore.FileSys.writeToFile({path:"c:\\file.txt", data:"this is what I want to save"});
However, you'll rarely want to save data to the root of the C: drive so that example isn't very useful in the real world.

Saving Data in Useful Places

SWF Studio defines some file monikers that you can use to refer to commonly used locations without knowing exactly where they are. You can use a file moniker in place of a file system path in any ActionScript API parameter that says: "This parameter supports monikers".

To save a file on the current users desktop:

ssCore.FileSys.writeToFile({path:"desktop://file.txt", data:"this is what I want to save"});
To save a file in "C:\Documents and Settings\USERNAME\Application Data":

ssCore.FileSys.writeToFile({path:"appdata://file.txt", data:"this is what I want to save"});
To save a file in folder your EXE was started from:

ssCore.FileSys.writeToFile({path:"startdir://file.txt", data:"this is what I want to save"});
Adding Data to an Existing File

Adding data to the end of an existing file is also a one liner.

ssCore.FileSys.appendToFile({path:"c:\\file.txt", data:"add this to the end of fhe file"});
If the file does not exist, FileSys.appendToFile will create it. If the file already exists it will just add the text you provide to the end of the file. This is really convenient because you don't have to know what's in the file ahead of time, you can just add more text to the end.

FileSys.appendToFile does not automatically add line breaks to the file. If you want to create a file with multiple lines, you need to add the line breaks yourself. A standard CRLF (carriage return and line feed) pair can be added by placing "\r\n" in a string literal, like this:

ssCore.FileSys.writeToFile({path:c:\\test.txt", data:"this will be line 1\r\n"});
ssCore.FileSys.appendToFile({path:c:\\test.txt", data:"i am line 2\r\n"});
ssCore.FileSys.appendToFile({path:c:\\test.txt", data:"3rd line in the file\r\n"});
ssCore.FileSys.appendToFile({path:c:\\test.txt", data:"etc.\r\n"});