Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Silverlight IsolatedFileStorage Delete Directory

Posted on 2010-09-02
2
Medium Priority
?
1,088 Views
Last Modified: 2013-11-12
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:

    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

Open in new window


This code returns:


{System.IO.IsolatedStorage.IsolatedStorageException: Unable to create directory.
   at System.IO.IsolatedStorage.IsolatedStorageFile.DeleteDirectory(String dir)
   at ExecuKeysSL.Helper.FileManager.ClearDirectory(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

Open in new window


The problem with this code is that isoStore.GetFileNames(Directory & "*") 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:


?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.
0
Comment
Question by:kmcbrearty
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 3

Author Comment

by:kmcbrearty
ID: 33590797
Ok, so after I posted this question I was able to get a working solution by modifying the code slightly.  It looks like the method will only return files from within a specified directory.  If you want to get all of the files in a directory you need to complete the path.  The other change to the code was adding the directory to the path when passing it to the deletefile method.  I wasn't sure if this would be included in the file name or not.  I am still curious as to why DeleteDirectory will not work but I have a working solution.
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(Directory & "\\" & File)
            Next
        End If
    End Sub

Open in new window

0
 
LVL 27

Accepted Solution

by:
MikeToole earned 2000 total points
ID: 33596887
In the msdn documentation for the IsolatedStorageFile.DeleteDirectory Method :
"A directory must be empty before it is deleted... "

The documentation of the deletefile method at:
    http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefile.deletefile.aspx
suggested the attached code to clear a directory.



                Dim fileNames As String() = isoFile.GetFileNames("*")
                ' List the files currently in this Isolated Storage.
                ' The list represents all users who have personal
                ' preferences stored for this application.
                If fileNames.Length > 0 Then
                    For Each name In fileNames
                        ' Delete the files.
                        isoFile.DeleteFile(name)
                    Next name
                    'Confirm no files are left.
                    fileNames = isoFile.GetFileNames("*")
                End If

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question