繁体   English   中英

在wp7中重置应用程序设置

[英]Reset Application setting in wp7

我创建了一个应用程序,该应用程序最初创建数据库并在其中保存一些数据。 现在,我想在用户单击“重置”按钮时删除此数据库及其文件,但出现错误–“此操作正在另一进程中使用”。 单击重置按钮时,我希望它删除并重新创建数据库。 有任何想法吗?

最常见的原因是与Windows Phone上的隔离存储进行交互的线程不安全特性。 无论您如何实现数据库(在一个文件或一系列文件中),都在某种程度上与隔离存储进行交互。

我强烈建议您阅读,并确保您在开始之前了解一下隔离存储的概述

您备注:

这是在另一个过程中使用的

让我觉得您正在使用第三方库来完成数据库工作。 当库本身无法访问隔离存储时,将引发此异常/错误。 在不确切知道如何实现数据库的情况下,很难准确地说明自己的情况。

您永远不会“重新创建IsolatedStorage”,Isolated Storage是一个术语,用于定义您的应用程序可以访问的磁盘空间的集合。 就像文件夹一样,该磁盘空间具有根目录,并且仅包含您创建的文件。

为了避免在访问隔离存储时出现线程异常,请确保在C#中使用using关键字,如下所示:

namespace IsolatedStorageExample
{
    public class ISOAccess
    {
        // This example method will read a file inside your Isolated Storage.
        public static String ReadFile(string filename)
        {
            string fileContents = "";
            // Ideally, you should enclose this entire next section in a try/catch block since
            // if there is anything wrong with below, it will crash your app.
            // 
            // This line returns the "handle" to your Isolated Storage. The phone considers the
            // entire isolated storage folder as a single "file", which is why it can be a 
            // little bit of a confusing name.
            using(IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForAppliaction())
            {   
                // If the file does not exist, return an empty string
                if(file.Exists(filename))
                {
                    // Obtain a stream to the file
                    using(IsolatedStorageFileStream stream = File.OpenFile(filename, FileMode.Open)
                    {
                        // Open a stream reader to actually read the file.
                        using(StreamReader reader = new StreamReader(stream))
                        {
                            fileContents = reader.ReadToEnd();
                        }
                    }
                }   
            }

            return fileContents;
        }
    }
}

那应该有助于解决线程安全性问题。 为了更具体地帮助您实现目标,请看一下以下方法(您可以将其添加到上面的类中):

// BE VERY CAREFUL, running this method will delete *all* the files in isolated storage... ALL OF THEM
public static void ClearAllIsolatedStorage()
{
    // get the handle to isolated storage
    using(IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
    {
        // Get a list of all the folders in the root directory
        Queue<String> rootFolders = new Queue<String>(file.GetDirectoryNames());

        // For each folder...
        while(0 != rootFolders.Count)
        {
            string folderName = rootFolders.Dequeue();

            // First, recursively delete all the files and folders inside the given folder.
            // This is required, because you cannot delete a non-empty directory
            DeleteFilesInFolderRecursively(file, folderName);

            // Now that all of it's contents have been deleted, you can delete the directory
            // itsself.
            file.DeleteDirectory(rootFolders.Dequeue());
        }

        // And now we delete all the files in the root directory
        Queue<String> rootFiles = new Queue<String>(file.GetFileNames());
        while(0 != rootFiles.Count)
            file.DeleteFile(rootFiles.Dequeue());
    }
}

private static void DeleteFilesInFolderRecursively(IsolatedStorageFile iso, string directory)
{
    // get the folders that are inside this folder
    Queue<string> enclosedDirectories = new Queue<string>(iso.GetDirectoryNames(directory));

    // loop through all the folders inside this folder, and recurse on all of them
    while(0 != enclosedDirectories.Count)
    {
        string nextFolderPath = Path.Combine(directory, enclosedDirectories.Dequeue());
        DeleteFilesInFolderRecursively(nextFolderPath);
    }

    // This string will allow you to see all the files in this folder.
    string fileSearch = Path.Combine(directory, "*");

    // Getting the files in this folder
    Queue<string> filesInDirectory = iso.GetFileNames(fileSearch);

    // Finally, deleting all the files in this folder
    while(0 != filesInDirectory.Count)
    {
        iso.DeleteFile(filesInDirectory.Dequeue());
    }
}

我强烈建议的另一件事是实现使用“多线程单例模式”来访问IsolatedStorage的类,如此处所述

希望对您有所帮助。 代码是“按原样”提供的,我没有编译它,但是一般概念都存在,因此,如果有什么不对,请阅读MSDN文档,以了解有什么不对劲的地方。 但我向您保证,大部分代码都是从我的功能代码复制而来的,因此它应该可以在很少幻想的情况下正常运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM