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:
public static void ClearFolder(String sPath, IsolatedStorageFile appStorage)
{
string[] filenames = GetFilenames(sPath);
if (filenames != null)
{
foreach (string sFile in filenames)
{
DeleteFile(System.IO.Path.Combine(sPath, sFile));
}
}
try
{
foreach (string sDirectory in appStorage.GetDirectoryNames(sPath))
{
ClearFolder(System.IO.Path.Combine(sPath, sDirectory) + @"\", appStorage);
}
}
catch (DirectoryNotFoundException ex)
{
return;
}
try
{
appStorage.DeleteDirectory(sPath);
}
catch (ArgumentException ex) { }
}
public static void DeleteFile(string sPath)
{
using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
appStorage.DeleteFile(sPath);
String sDirectory = System.IO.Path.GetDirectoryName(sPath);
if (appStorage.GetFileNames(sPath).Length == 0)
{
appStorage.DeleteDirectory(sDirectory);
}
}
}
public static string[] GetFilenames(string sDirectory)
{
using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
try
{
return appStorage.GetFileNames(sDirectory);
}
catch (DirectoryNotFoundException)
{
return null;
}
}
}