Ryan K
Ryan K

Reputation: 351

How can you delete a file in the application directory?

We are writing log files to File.applicationDirectory and want to clean out old log files. The question is how do you delete files out of that directory? When I try and delete old log files out of this directory it gives a security exception. Is there anyway around this?

I know you are not supposed to write to File.applicationDirectory, but we are writting are logs there anyways. So please don't just say don't do that!

Thanks,

Ryan

Upvotes: 0

Views: 3154

Answers (3)

zzeldi
zzeldi

Reputation: 21

Another trick, for security workaround

var fileLoc:File= File.applicationDirectory.resolvePath("some.log");
var file:File=new File(fileLoc.nativePath);
fileStream.open(file,FileMode.WRITE);

The second file object can alter the file withot any problem.

Upvotes: 2

Ryan K
Ryan K

Reputation: 351

I found the answer shortly after posting. I was looking in FileTarget of how they write the log file into the application directory and found this gem:

        var logFile:File = this._logDirectory.resolvePath(filename);
        logFile = new File(logFile.nativePath); // Hack to get around SecurityError if log directory exists within the application directory

So you just specify the full native path and not a relative path from the application directory and you can do whatever you want. Interesting security model!

Upvotes: 1

Umesh
Umesh

Reputation: 1242

File.applicationDirectory is a read only directory.

http://livedocs.adobe.com/flex/3/langref/flash/filesystem/File.html

Upvotes: 1

Related Questions