kmcbrearty
asked on
Silverlight IsolatedFileStorage Delete Directory
Basically, I am caching data while the application is running to improve performance but if the application is reloaded I want the application to cache everything again. I currently have methods for caching the information to a file, reading it back in, or going to my web service to retrieve the information. The logic for this code is:
IF cache file exists THEN
Load Data From Cache
ELSE
Retrieve data from Web Service
Cache Data
END IF
With this in mind I thought the perfect way of clearing the cache would be to add code to the Application_Startup event in the App.xaml file to delete everything in the isolated file storage before the application ties to load. I tried to accomplish with the following code:
This code returns:
I am unsure why I am getting an "Unable to create directory" error when I am trying to delete a directory. I also checked to see what existed in isoStore.GetDirectoryNames :
Also note that I am running a test to verify that the directory exists before the line of code to delete the directory is run.
Ok, so I figured I would try to work around this issue with the following code:
The problem with this code is that isoStore.GetFileNames(Dire ctory & "*") doesn't return any files even though they exist. I even tried to see what was returned if I just asked for all of the files:
The last thing that I tried was to run isoStore.Remove which ran successfully. The problem with running isoStore.Remove though is that eventually I am planning on storing additional information here besides cached results. When I do I will not want this information deleted.
Any help that can be provided in resolving this would be greatly appreciated.
IF cache file exists THEN
Load Data From Cache
ELSE
Retrieve data from Web Service
Cache Data
END IF
With this in mind I thought the perfect way of clearing the cache would be to add code to the Application_Startup event in the App.xaml file to delete everything in the isolated file storage before the application ties to load. I tried to accomplish with the following code:
Private Shared Sub ClearDirectory(ByVal Directory As String)
Dim isoStore As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication
If isoStore.DirectoryExists(Directory) Then
isoStore.DeleteDirectory(Directory)
End If
End Sub
This code returns:
{System.IO.IsolatedStorage.IsolatedS torageExce ption: Unable to create directory.
at System.IO.IsolatedStorage.IsolatedSt orageFile. DeleteDire ctory(Stri ng dir)
at ExecuKeysSL.Helper.FileManager.Clear Directory( String Directory)
...........
}
I am unsure why I am getting an "Unable to create directory" error when I am trying to delete a directory. I also checked to see what existed in isoStore.GetDirectoryNames
?isostore.GetDirectoryNames
{Length=1}
(0): "EKData"
Also note that I am running a test to verify that the directory exists before the line of code to delete the directory is run.
Ok, so I figured I would try to work around this issue with the following code:
Private Shared Sub ClearDirectory(ByVal Directory As String)
Dim isoStore As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication
If isoStore.DirectoryExists(Directory) Then
Dim files As String() = isoStore.GetFileNames(Directory & "*")
For Each File In files
isoStore.DeleteFile(File)
Next
End If
End Sub
The problem with this code is that isoStore.GetFileNames(Dire
?isostore.GetFileNames("*")
{Length=0}
The last thing that I tried was to run isoStore.Remove which ran successfully. The problem with running isoStore.Remove though is that eventually I am planning on storing additional information here besides cached results. When I do I will not want this information deleted.
Any help that can be provided in resolving this would be greatly appreciated.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Open in new window