What is the fastest way to clear isolated storage for a Windows Phone 7 app?

I am currently working on an application that writes data to IsolStorageStore. As part of the application, I would like to implement the "clear all data / reset" button, but listing all the files that exist and all existing folders takes quite a lot of time. Is there a magic “reset” method or something that I can use, or should I focus on optimizing the removal process manually?

Or can I leave without providing such functionality and leave it to the user to remove / reinstall the application to reset?

My exception removal method is all the files below:

    /// <summary>
    /// deletes all files in specified folder
    /// </summary>
    /// <param name="sPath"></param>
    public static void ClearFolder(String sPath, IsolatedStorageFile appStorage)
    {    
        //delete all files
        string[] filenames = GetFilenames(sPath);
        if (filenames != null)
        {
            foreach (string sFile in filenames)
            {
                DeleteFile(System.IO.Path.Combine(sPath, sFile));
            }
        }

        //delete all subfolders if directory still exists
        try
        {
            foreach (string sDirectory in appStorage.GetDirectoryNames(sPath))
            {
                ClearFolder(System.IO.Path.Combine(sPath, sDirectory) + @"\", appStorage);
            }
        }
        catch (DirectoryNotFoundException ex)
        {
            //current clearing folder was deleted / no longer exists - return
            return;
        }

        //try to delete this folder
        try
        {
            appStorage.DeleteDirectory(sPath);
        }
        catch (ArgumentException ex) { }

    }

    /// <summary>
    /// Attempts to delete a file from isolated storage - if the directory will be empty, it is also removed.
    /// </summary>
    /// <param name="sPath"></param>
    /// <returns></returns>
    public static void DeleteFile(string sPath)
    {
        using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            appStorage.DeleteFile(sPath);

            String sDirectory = System.IO.Path.GetDirectoryName(sPath);
            //if this was the last file inside this folder, remove the containing folder
            if (appStorage.GetFileNames(sPath).Length == 0)
            {
                appStorage.DeleteDirectory(sDirectory);
            }
        }
    }

    /// <summary>
    /// Returns an array of filenames in a given directory
    /// </summary>
    /// <param name="sHistoryFolder"></param>
    /// <returns></returns>
    public static string[] GetFilenames(string sDirectory)
    {
        using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            try
            {
                return appStorage.GetFileNames(sDirectory);
            }
            catch (DirectoryNotFoundException)
            {
                return null;
            }
        }
    }
+3
1

Remove().

:

using (var store = IsolatedStorageFile.GetUserStoreForApplication()) 
{ 
    store.Remove();
}
+6

Source: https://habr.com/ru/post/1768708/


All Articles