清除 Windows Phone 7 应用程序的独立存储的最快方法是什么?
我目前正在开发一个将数据写入IsolatedStorageStore 的应用程序。作为应用程序的一部分,我想实现一个“清除所有数据/重置”按钮,但枚举所有存在的文件和所有存在的文件夹需要相当多的时间。是否有神奇的“重置”方法或我可以使用的东西,或者我应该专注于优化手动删除过程?
或者我可以不提供此类功能,并让用户卸载/重新安装应用程序进行重置吗?
我可怕的删除所有文件方法如下:
/// <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;
}
}
}
I'm currently working on an application which writes data to the IsolatedStorageStore. As part of the app, I'd like to implement a "clear all data/reset" button, but enumerating through all the files that exist and all the folders that exist is taking quite a bit of time. Is there a magic "reset" method or something I can use, or should I instead focus on optimizing the manual deletion process?
Or can I get away with not providing such functionality, and leave it to the user to uninstall/reinstall the application for a reset?
My hideous delete-all-files method is 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;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在寻找 Remove() 方法。
像这样使用它:
You're looking for the Remove() method.
Use it like this: