清除 Windows Phone 7 应用程序的独立存储的最快方法是什么?

发布于 2024-09-26 21:32:26 字数 2740 浏览 0 评论 0原文

我目前正在开发一个将数据写入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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情定在深秋 2024-10-03 21:32:26

您正在寻找 Remove() 方法。

像这样使用它:

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

You're looking for the Remove() method.

Use it like this:

using (var store = IsolatedStorageFile.GetUserStoreForApplication()) 
{ 
    store.Remove();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文