以檔案系統的方式存取 Isolated Storage中的檔案

以檔案系統的方式存取 Isolated Storage中的檔案

 
在很多時候,你的程式都需要儲存,處理後的資料,例如記事本,網頁內容等…

Silverlight For Windows Phone 提供了相同的儲存的架構 “獨立的儲存體”(Isolated Storage), 它會將資料儲存在獨立的實體記憶單元中,並且也獨立(Isolated)於每個應用程式,意思是說

“每個應用程式只能夠存取自己產生的資料,而其他的應用程式產生的資料是無法(也不知道)存取的”, 舉個例子: 一個記事本應用程式,無法開啟另一個程式(MP3播放器產生的mp3清單資料

如果程式移除後,因為該資料屬於程除的程式,所以也會一併的從系統中移除,怎麼備份 ~~ 目前好像只有藉由雲端(Cloud)進行備份。

我們以檔案系統的方式存取 Isolated Storage 需要使用 IsolatedStorageFile 類別來存取資料

             IsolatedStorageFile 類別,在 namespace System.IO.IsolatedStorage

片段程式:

儲存資料:

   1:  try 
   2:    { 
   3:      using (var isostorage = IsolatedStorageFile.GetUserStoreForApplication()) 
   4:      using (var filestream = new IsolatedStorageFileStream("TestFile.Txt", 
   5:                              FileMode.Create,  
   6:                              FileAccess.Write,  
   7:                              isostorage)) 
   8:      { 
   9:        StreamWriter SWriter = new StreamWriter(filestream); 
  10:        writer.Write("This is TestFile Content "); 
  11:        writer.Close(); 
  12:      } 
  13:    } 
  14:    catch (Exception) 
  15:    { 
  16:      MessageBox.Show("Save Fail"); 
  17:    } 

讀取資料:

   1:  string filename ;
   2:  string fileContent ;
   3:  try   
   4:    {   
   5:     using (var isostorage = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication())   
   6:     using (var filestream= new solatedStorageFileStreamfilename,
   7:                                          FileMode.Open,
   8:                                          FileAccess.ReadWrite, isostorage))  
   9:    {    
  10:        StreamReader sreader = new StreamReader(stream);   
  11:           fileContent = reader.ReadToEnd(); 
  12:          filename = filename;  
  13:       reader.Close();     }    }catch(Exception) 
  14:    {  
  15:    MessageBox.Show("Load Fail");  
  16:    }

 

IsolatedStorageFileStream

針對參數補充(參考自 Microsoft MSDN)

a. FileMode

Specifies how the operating system should open a file.

Namespace: System.IO

CreateNew Specifies that the operating system should create a new file.
Create Specifies that the operating system should create a new file. If the file already exists, it will be overwritten.
Create is equivalent to requesting that if the file does not exist, use CreateNew; otherwise, use Truncate
Open Specifies that the operating system should open an existing file. The ability to open the file is dependent on the value specified
by
FileAccess. A System.IO.FileNotFoundException is thrown if the file does not exist
OpenOrCreate Specifies that the operating system should open a file if it exists; otherwise, a new file should be created
Truncate Specifies that the operating system should open an existing file. Once opened, the file should be truncated so that its size is zero bytes.
Append Opens the file if it exists and seeks to the end of the file, or creates a new file. Append can only be used in conjunction with Write.
Attempting to seek to a position before the end of the file will throw an
IOException and any attempt to read fails and throws an NotSupportedException.


b.FileAccess

Defines constants for read, write, or read/write access to a file.

Namespace: System.IO

Read Read access to the file. Data can be read from the file. Combine with Write for read/write access.
Write Write access to the file. Data can be written to the file. Combine with Read for read/write access.
ReadWrite Read and write access to the file. Data can be written to and read from the file.