Windows 8 Metro Style App中檔案操作都包含在Windows.Storage命名空間中,其中包括StorageFolder,StorageFile,FileIO等類庫。
檔案對象用StorageFile實現,檔案頭用StorageFolder實現,下面看下具體的用法及各類庫下的屬性他方法。
建立檔案
StorageFolder storageFolder=KnownFolders.DocumentsLibrary; StorageFile storageFile=await storageFolder.CreateFileAsync("sample.txt",CreationCollisionOption.ReplaceExisting);
KnownFolders提供了系統中常規的檔案路徑。
CreationCollisionOption提供了建立檔案衝突時的操作選項。
FileIO對象負責檔案的讀/寫操作
讀取檔案
string fielContent=await FileIO.ReadTextAsync(storageFile);
讀取操作包含三種方式:
返回普通文本 ReadTextAsync(storageFile)/ReadTextAsync(storageFile,UnicodeEncoding)(返回指定的文本編碼格式)
返迴流 ReadBufferAsync(storageFile)
IBuffer buffer = await FileIO.ReadBufferAsync(storageFile); using (DataReader dataReader = DataReader.FromBuffer(buffer)) { string fileContent = dataReader.ReadString (buffer.Length); }
還可通過流讀取檔案內容
using (IRandomAccessStream readStream = await sampleFile.OpenAsync(FileAccessMode.Read)) { using (DataReader dataReader = new DataReader(readStream)) { uint numBytesLoaded = await dataReader.LoadAsync((uint)readStream.Size); string fileContent = dataReader.ReadString(numBytesLoaded); } }
返迴文本行ReadLinesAsync(storageFile)/ReadLinesAsync(storageFile,UnicodeEncoding)
寫入檔案
await FileIO.WriteTextAsync(storageFile,content)
寫入檔案也包含以下幾種操作:
寫入Buffer資料 WriteBufferAsync(IStorage File,IBuffer buffer)
寫入bytes位元組資料 WriteBytesAsync(IStorage File,bytes[] byte)
寫入文本行 WriteLinesAsync(IStorageFile File, IIterable(String)) /WriteLinesAsync(IStorageFile File, IIterable(String), UnicodeEncoding)
寫入字串 WriteTextAsync(IStorageFile File,string content)
通過流寫入內容
using (IRandomAccessStream writeStream = await storageFile.OpenAsync(FileAccessMode.ReadWrite)) { using (DataWriter dataWriter = new DataWriter(writeStream)) { dataWriter.WriteString(userContent); await dataWriter.StoreAsync(); await dataWriter.FlushAsync(); } }
同樣也提供追加文本的方法
AppendLinesAsync(IStorageFile, IIterable(String)) /AppendLinesAsync(IStorageFile, IIterable(String), UnicodeEncoding)
AppendTextAsync(IStorageFile, String) /AppendTextAsync(IStorageFile, String, UnicodeEncoding)
刪除檔案
await storageFile.DeleteAsync()
複製檔案
StorageFile storageFileCopy = await storageFile.CopyAsync(KnownFolders.DocumentsLibrary, "sample - Copy.txt", NameCollisionOption.ReplaceExisting);
複製文本也有幾種方式
CopyAndReplaceAsync
CopyAsync(IStorageFolder)
CopyAsync(IStorageFolder, String FileName)
CopyAsync(IStorageFolder, String FileName, NameCollisionOption)
重新命名檔案
StorageFile storageFile1=await storageFile.RenameAsync("sampleRe.txt")
移動檔案
await storageFile.MoveAsync(StorageFolder NewStorageFolder,string NewFileName);
提到檔案操作,不得不說說檔案流。
在以前的開發中,我們經常用到的就是檔案流、位元組數組等操作,在WinRT中有些區別,檔案流有IRandomAccessStream、FileInputStream、FileOutStream、FileRandomAccessStream等,用IBuffer代替了byte[]。下面看看具體的用法:
擷取檔案流
IRandomAccessStream readStream = await sampleFile.OpenAsync(FileAccessMode.Read)FileInputStream inputStream=readStream.GetInputStreamAt(0) as FileInputStream;FileOutputStream outStream=readStream.GetOutputStreamAt(0) as FileOutputStream;
DataReader/DataWriter的用法見上
IBuffer、byte[]、Stream之間的轉換
IBuffer buffer = await FileIO.ReadBufferAsync(sampleFile);byte[] bytes=WindowsRuntimeBufferExtensions.ToArray(buffer,0,(int)buffer.Length);Stream stream = WindowsRuntimeBufferExtensions.AsStream(buffer);
還有很多檔案操作的情境沒有整理,雖然WinRT中提供了很多檔案流,但它們的核心用法都是類似 的,希望在後續的不斷使用中慢慢接觸不同類型流的使用。