2012-03-03 24 views
3

IsolatedStorageからテキストファイルを読み込み、文字列が含まれていることを確認しようとしています。そうでない場合、文字列はファイルの末尾に追加されます。しかし、文字列をファイルに書き込もうとすると、エラーが発生します:"Operation not permitted on IsolatedStorageFileStream."。私のコードは以下の通りです。どうすればこの問題を解決できますか?エラー "OperationはIsolatedStorageFileStreamで許可されていません。" wp7

私はあなたが持った問題は、あなたがNEWINGでIsolatedStorageFileStreamをのStreamWriterを作成ラインに関係していると思います
public void AddToDownloadList() 
{ 
    IsolatedStorageFile downloadFile=IsolatedStorageFile.GetUserStoreForApplication(); 

    try 
    { 
     string downloads = string.Empty; 

     if (!downloadFile.DirectoryExists("DownloadedFiles")) 
      downloadFile.CreateDirectory("DownloadedFiles"); 

     if(downloadFile.FileExists("DownloadedFiles\\DownloadList.txt")) 
     { 
      IsolatedStorageFileStream downloadStream = downloadFile.OpenFile("DownloadedFiles\\DownloadList.txt",FileMode.Open, FileAccess.Read); 

      using (StreamReader reader = new StreamReader(downloadStream)) 
      { 
       downloads = reader.ReadToEnd(); 
       reader.Close(); 
      } 
      downloadFile.DeleteFile("DownloadedFiles\\DownloadList.txt"); 
     } 

     downloadFile.CreateFile("DownloadedFiles\\DownloadList.txt"); 

     string currentFile = FileName; 

     if (!downloads.Contains(currentFile)) 
     { 
      downloads += currentFile; 

      using (StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("DownloadedFiles\\DownloadList.txt", FileMode.Create, FileAccess.Write, downloadFile))) 
      { 
       writeFile.Write(currentFile + ","); 
       writeFile.Close(); 
      } 
     } 
    } 

    catch (Exception ex) 
    { 
     string message = ex.Message; 
    } 
} 

答えて

5

- あなたはすでにdownloadFile.CreateFile()呼び出しの戻り値から正しいものを持っている必要がある場合。

私はそれはあなたが何をしたいのかないと思うが、このコードを試してみてください。

public static void AddToDownloadList() 
{ 
    try 
    { 
     AddToDownloadList("DownloadedFiles", "this file name", "DownloadedFiles\\DownloadList.txt"); 
    } 
    catch (Exception ex) 
    { 
     System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message); 
    } 
} 

public static void AddToDownloadList(string directory, string fileName, string filePath) 
{ 
    string downloads = string.Empty; 
    using (IsolatedStorageFile downloadFile = IsolatedStorageFile.GetUserStoreForApplication()) 
    { 
     if (!downloadFile.DirectoryExists(directory)) 
      downloadFile.CreateDirectory(directory); 


    if (downloadFile.FileExists(filePath)) 
     { 
      IsolatedStorageFileStream downloadStream = downloadFile.OpenFile(filePath, FileMode.Open, FileAccess.Read); 
      using (StreamReader reader = new StreamReader(downloadStream)) 
      { 
       downloads = reader.ReadToEnd(); 
       reader.Close(); 
      } 
     } 

     string currentFile = fileName; 
     if (!downloads.Contains(currentFile)) 
     { 
      downloadFile.DeleteFile(filePath); 
      using (IsolatedStorageFileStream stream = downloadFile.CreateFile(filePath)) 
      { 

       downloads += currentFile; 
       using (StreamWriter writeFile = new StreamWriter(stream)) 
       { 
        writeFile.Write(currentFile + ","); 
        writeFile.Close(); 
       } 

      } 
     } 
    } 
} 
関連する問題