2012-04-16 8 views
2

私はデータベースを最初に作成し、そこにデータを保存するアプリケーションを作成しました。 ユーザーがリセットボタンをクリックしたときにこのデータベースとそのファイルを削除しますが、「これは別のプロセスで使用されています」というエラーが表示されます。私はそれを削除し、リセットボタンをクリックすると、データベースを再作成したい。何か案は?wp7でのアプリケーション設定のリセット

+0

どのようにファイルを削除しますか?ファイル名を指すすべてのDbContextインスタンスを閉じて処分しましたか? –

+0

あなたはどのくらいのデータを取得していますか?多分ApplicationSettingsは十分です。 –

+0

IsolatedStorageデータベースを再作成し、イメージファイルとオーディオファイルを削除する –

答えて

0

この原因の最も一般的な原因は、スレッドにあります。安全でない Windows Phoneで隔離されたストレージとのやり取りの性質。データベースをどのように実装していても(ファイルや一連のファイルの中にあっても)、あるレベルで独立したストレージとやりとりしています。

私は非常にお読みになり、遠くに行く前にthis overview of isolated storageを理解していることを強くお勧めします。

あなただ発言:

これは

は私はあなたのデータベースのものを行うために、サードパーティのライブラリを使用していると思わせる別のプロセスで使用されています。この例外/エラーは、ライブラリ自体が独立したストレージにアクセスできない場合にスローされます。どのようにデータベースを実装しているかを正確に知らなければ、あなたの状況に正確に話すのは難しいです。

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; 
     } 
    } 
} 

を。私は非常にお勧めしますもう一つは、使用してIsolatedStorageにアクセスするクラスを実装している

// 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()); 
    } 
} 

:あなたが何をしたいかに向けて、より具体的に参考にするには、以下の方法(あなたが上記のクラスにこれを追加することができます)を見てみましょう記載されている "マルチスレッドシングルトンパターン" here.

希望です。コードは「現状のまま」提供されていますが、私はそれをコンパイルしていませんが、一般的な概念はすべてそこにありますので、何か問題があれば、MSDNドキュメントを読んで私がどこにいるのか見てください。しかし、私はあなたに保証します、これはほとんどが私の機能コードからコピーされているので、ほんの少しのファンガガリングで適切に動作するはずです。

関連する問題