2

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

4

1 回答 1

0

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

我强烈建议您阅读,并确保您了解隔离存储的概述,然后再走得太远。

你说:

这是在另一个进程中使用

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

您永远不会“重新创建独立存储”,独立存储是用于定义应用程序可以访问的磁盘空间集合的术语。与文件夹非常相似,此磁盘空间有一个根目录,并且仅包含您创建的文件。

为了避免在访问独立存储时出现线程异常,请确保在 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());
    }
}

我强烈推荐的另一件事是使用此处描述的“多线程单例模式”实现访问独立存储的类

希望这会有所帮助。代码是“按原样”提供的,我没有编译它,但一般概念都在那里,所以如果有什么不对劲,请阅读 MSDN 文档,看看我哪里出错了。但我向你保证,其中大部分内容都是从我的功能代码中复制而来的,因此它应该可以正常工作,并且很少有任何煽动性。

于 2012-04-25T02:44:01.830 回答