Windows 8 Metro Stype App 学习笔记(五)--文件操作
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); } }
写入文件
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中提供了很多文件流,但它们的核心用法都是类似 的,希望在后续的不断使用中慢慢接触不同类型流的使用。
posted on 2012-04-09 21:42 ShinyTang 阅读(6136) 评论(6) 编辑 收藏 举报