2011-12-30 60 views
1

我可以在獨立存儲資源管理器中創建文件夾,但不能將文件寫入該文件夾。當我使用如下代碼:如何在windows phone 7應用程序中的獨立存儲資源管理器中創建文件夾並將文件寫入該文件夾?

IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication(); 
store.CreateDirectory("JSON"); 
using (var isoFileStream = new IsolatedStorageFileStream("JSON\\dd.txt", FileMode.OpenOrCreate, store)) 
{ 
    using (var isoFileWriter = new StreamWriter(isoFileStream)) 
    { 
     isoFileWriter.WriteLine(jsonFile); 
    } 
} 

只創建該文件夾,但該文件夾中沒有文件。請提供用於在獨立存儲資源管理器中創建文件夾的示例代碼,並將文件寫入該文件夾。這是一個WP7應用程序。

回答

0

您是否嘗試過直接使用isoFileStream.Write,而不是使用StreamWriter對象isoFileWriter。

請使用下面的代碼,並嘗試

IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication(); 
store.CreateDirectory("JSON"); 
using (var isoFileStream = new IsolatedStorageFileStream("JSON\\dd.txt", FileMode.OpenOrCreate, store)) 
{ 
    isoFileStream.Write(jsonFile); 
} 
0

嘗試是這樣的:

// Obtain the virtual store for the application. 
    IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication(); 
    iso.CreateDirectory("Database"); 
    // Create stream for the file in the installation folder. 
    using (Stream input = Application.GetResourceStream(new Uri("test.sdf", UriKind.Relative)).Stream) 
    { 
     // Create stream for the new file in the isolated storage 
     using (IsolatedStorageFileStream output = iso.CreateFile("Database\\test.sdf")) 
     { 
      // Initialize the buffer 
      byte[] readBuffer = new byte[4096]; 
      int bytesRead = -1; 

      // Copy the file from installation folder to isolated storage. 
      while((bytesRead = input.Read(readBuffer, 0, readBuffer.Length)) > 0) 
      { 
       output.Write(readBuffer, 0, bytesRead); 
      } 
     } 
    } 

此代碼是與我相似,我用下分離,因而能夠從應用程序安裝目錄文件夾數據庫複製到特定的文件夾存儲。希望它會幫助你一些靈感:)

相關問題