views:

259

answers:

1

Hi, I'm using IsloatedStorage in a Silverlight app to log information on the client, and I added a function to clear the log file... However I have had problems with the two approaches I tried:

Approach one: use

IsolatedStorageFile.DeleteFile("log.log");

Result: This fails and returns an "[IsolatedStorage_DeleteFile]" error (No other info). The function works fine on test files, e.g. DeleteFile("test.txt"), but refuses to delete the log. I though that perhaps the log is being used, and tried to close it with

IsolatedStorageFileStream.close()

but this returns a different error "[IsolatedStorage_StoreNotOpen]"... :( I know it is open as the previous line of code successfully logs a message

Approach Two: Reopen the log file using the Truncate file mode-

    _storageFileStream = new IsolatedStorageFileStream(logfilename, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite, _storageFile);

According to msdn, Truncate "Specifies that the operating system should open an existing file. Once opened, the file should be truncated so that its size is zero bytes." However, it opens my log file and fills it with blank space! The filesize is left identical, the next log message is appended to the end of all of the space.

Haven't found much useful info on these issues yet, cheers for any help!

A: 

Hmmm, I've found a way to do this, not by closing but by disposing:

IsolatedStreamWriter.Dispose();                       
IsolatedStorageFile.DeleteFile("log.log");
IsolatedStorageFileStream = new IsolatedStorageFileStream(logfilename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite, _storageFile);

Didn't get the 'truncate' approach to work, but no need now...

Donakello
Note that if you're using IsolatedStorage for a Windows Phone 7 app, the FileMode.Truncate is not supported anyway.
Blakomen