我创建了一个应用程序,它最初会创建一个数据库并在其中保存一些数据。 现在,我想在用户点击重置按钮时删除此数据库及其文件,但我收到错误 - “这是在另一个进程中使用”。我希望它在单击重置按钮时删除并重新创建数据库。有什么想法吗?
答案 0 :(得分:0)
最常见的原因是与Windows Phone上的独立存储交互的线程不安全性质。无论您如何实现数据库(无论是文件还是文件系列),您都会在某种程度上与隔离存储进行交互。
我强烈建议您阅读,并确保在走得太远之前理解this overview of isolated storage。
你说的是:
这在另一个过程中使用
让我觉得你正在使用第三方库来做你的数据库。当库本身无法访问隔离存储时,会抛出此异常/错误。如果不确切知道如何实施数据库,很难准确地说出你的情况。
您永远不会“重新创建IsolatedStorage”,“隔离存储”是一个术语,用于定义应用程序可以访问的磁盘空间集合。与文件夹非常相似,此磁盘空间具有根,并且仅包含您创建的文件。
为了在访问独立存储时避免线程异常,请确保在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的类,如here.所述
希望这有帮助。代码是“按原样”提供的,我没有编译它,但是一般的概念都在那里,所以如果有什么不妥之处,请阅读MSDN文档以查看我在哪里学习。但我向你保证,大部分是从我的功能代码中复制而来的,因此它应该可以正常工作,只需很少的粉丝。