Win8 Metro 文件的读写操作

对于数字图像处理而言,一张图像我们不一定非要对图像对象进行操作,多数情况我们要使用到二进制,文本等文件的读写,而Win8Metro中,我们不能在向以前那样调用WIN32的API函数来进行文件操作,因此,下面就来介绍一下 Win8 Metro中文件的读写操作。

1 Windows 8 Metro Style App中文件的操作都包含在Windows.Storage命名空间中,其中包括StorageFolder,StorageFile,FileIO等类库。

2 Win8文件的读写操作都是异步方式进行的,因此要使用async

3 创建文件:

StorageFile storageFile=await

Windows.Storage.KnownFolders.DocumentsLibrary.CreateFileAsync("1.txt",Windows.Storage.CreationCollisionOption.ReplaceExisting);

这里我们创建了一个1.txt的文档,如果已经存在这个文档,那么新建的文档将替换,覆盖掉旧文档。

  由于文档读写是异步方式操作,因此,我们要将它放到async修饰的函数里才可以使用,具体如下:

privateasyncvoid SelectImageOne(byte[]outArary)

        {

            StorageFile storageFile=await Windows.Storage.KnownFolders.DocumentsLibrary.CreateFileAsync("1.txt",Windows.Storage.CreationCollisionOption.ReplaceExisting);

            awaitFileIO.WriteBytesAsync(storageFile, outArary);

        }

  在上述的代码中,参数是我们要写入到文件“1.txt”里的内容,这里是一个byte[]数组。

4 写入文件:

  3中的代码所示awaitFileIO.WriteBytesAsync(storageFile, outArary);

  写入文件的方法是FileIO中的write方法,这里一共有以下四种方法:

  WriteBufferAsync(Windows.Storage.IStorageFile file, IBuffer buffer);

  WriteBytesAsync(Windows.Storage.IStorageFile file, byte[] buffer);

WriteLinesAsync(Windows.Storage.IStorageFile file, IEnumerable<string> lines);

WriteLinesAsync(Windows.Storage.IStorageFile file, IEnumerable<string> lines,

UnicodeEncoding encoding);

WriteTextAsync(Windows.Storage.IStorageFile file, string contents);

WriteTextAsync(Windows.Storage.IStorageFile file, string contents,

UnicodeEncoding encoding);

  这里我们列举的是写入byte[]的方法。

5 打开文件:

StorageFile storageFile=await

Windows.Storage.KnownFolders.DocumentsLibrary.CreateFileAsync("1.txt",Windows.Storage.CreationCollisionOption. OpenIfExists);

  这里我们打开了一个名字为”1.txt”的文本文件。

6 读取文件:

  FileIO中有三种文件读取方法,分别读取不同的文件:

awaitFileIO.ReadTextAsync(Windows.Storage.IStorageFile file);

awaitFileIO.ReadTextAsync(Windows.Storage.IStorageFile file, UnicodeEncoding encoding);//返回指定的文本编码格式

awaitFileIO. ReadBufferAsync (Windows.Storage.IStorageFile file);

awaitFileIO. ReadLinesAsync (Windows.Storage.IStorageFile file);

awaitFileIO. ReadLinesAsync (Windows.Storage.IStorageFile file, UnicodeEncoding encoding);

  这里我们以文本为例:

  string fileIContent = awaitFileIO. ReadTextAsync (storageFile);

  这样我们就返回了一个string文本。

  我们也可以通过流来读取文件:

IBuffer buffer = await FileIO.ReadBufferAsync(storageFile);

using (DataReader dataReader = DataReader.FromBuffer(buffer))

{
string fileContent = dataReader.ReadString (buffer.Length);

}

7 IBuffer, byte[], Stream之间的相互转换:

StorageFile storageFile=await

Windows.Storage.KnownFolders.DocumentsLibrary.CreateFileAsync("1.txt",Windows.Storage.CreationCollisionOption. OpenIfExists);

IBuffer buffer = await FileIO.ReadBufferAsync(storageFile);

byte[] bytes=WindowsRuntimeBufferExtensions.ToArray(buffer,0,(int)buffer.Length);

Stream stream = WindowsRuntimeBufferExtensions.AsStream(buffer);

8 错误总结:

  这个文件操作中最容易出现如下错误:

Error         1       App manifest contains a contract 'windows.fileTypeAssociation' without name on element 'FileTypeAssociation'.

出现如下情况,是因为我们没有将要打开的文件类型添加到系统中去,这时我们可以按如下步骤进行:

  首先,打开项目工程的Package.appxmanifest或者mainfest,界面如下:

Win8 Metro 文件的读写操作 - CSharp - C

 然后,点击Capabilities选项(比如我们要打开txt文档):

 

Win8 Metro 文件的读写操作 - CSharp - C

 

 我们以打开txt文件为例,我们要将Documents Library Access打上对勾。

  接着,我们点击Declarations选项,进入新界面如下:

Win8 Metro 文件的读写操作 - CSharp - C

  这里注意了,我们要点击Select one,在里面选择File Type Associations选项,然后点击Add添加,在右边的显示框中,我们要将Name设置为txt,将File type设置为文件后缀名(.txt),这样就可以了。

  最后点击保存设置,OK

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值