gpt4 book ai didi

c# - 在 wp7 中重置应用程序设置

转载 作者:太空宇宙 更新时间:2023-11-03 16:33:04 24 4
gpt4 key购买 nike

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

最佳答案

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

我强烈建议您阅读并确保您理解 this overview of isolated storage在走得太远之前。

你的评论:

This is in use in another process

让我觉得您正在使用第三方库来处理您的数据库。当库本身无法访问独立存储时,将抛出此异常/错误。在不确切了解您如何实现数据库的情况下,很难准确说明您的情况。

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

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

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 文档以查看我犯了什么错误。但我向你保证,其中大部分是从我的功能代码中复制的,因此它应该可以正常工作,只需很少的 fanagaling。

关于c# - 在 wp7 中重置应用程序设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10173742/

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