gpt4 book ai didi

optimization - 清除 Windows Phone 7 应用程序的 isolatedstoragestore 的最快方法是什么?

转载 作者:行者123 更新时间:2023-12-04 02:22:49 25 4
gpt4 key购买 nike

我目前正在开发一个将数据写入 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;
}
}
}

最佳答案

您正在寻找 Remove()方法。

像这样使用它:

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

关于optimization - 清除 Windows Phone 7 应用程序的 isolatedstoragestore 的最快方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3894715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com