2012-03-03 105 views
3

我想從IsolatedStorage讀取一個文本文件,並檢查它是否包含一個字符串。如果不是,則將該字符串添加到文件的末尾。但是當我試圖將字符串寫入文件時,出現錯誤:"Operation not permitted on IsolatedStorageFileStream."。我的代碼如下所示。我怎樣才能克服這個問題?錯誤「IsolatedStorageFileStream不允許操作」。 wp7

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

我覺得您遇到的問題已經做在其中創建由newing了IsolatedStorageFileStream的StreamWriter的行 - 當你已經應該從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(); 
       } 

      } 
     } 
    } 
} 
相關問題