win8.1 文件读写(一)

在windows store app 开发过程中,经常需要进行文件的读写操作,下面给出的是一些简单的文件读写,仅供参考


1.简单的文件写入操作

private async void write()

{

    StorageFolder storagefolder = ApplicationData.Current.LocalFolder;//获取文件夹路径

    StorageFile storagefile = null;

    storagefile = await storagefolder .CreateFileAsync(“Me.txt”, CreationCollisionOption.ReplaceExisting);//在指定路径下创建文件(可重写)

    if (storagefile !=null)

    {

        using (Stream stream = await storagefile.OpenStreamForWriteAsync())

        {

            byte[] content= Encoding.UTF8.GetBytes(“Hello World !”);

            await stream.WriteAsync(content, 0, content.Length);//数据写入

        }

    }

    else

    {

        //文件没找到

    }

}

这样,到程序的工作目录下,就可以看到一个名为Me.txt的文本文件,打开文件,明晃晃的几个大字:Hello World !

我们既然已经可以将数据写入到文件中,那么又如何读取文件中的内容呢,我们继续文件的读取操作

2.简单的文件读取操作

private async void read()

{

    StorageFolder storagefolder = ApplicationData.Current.LocalFolder;//获取文件夹路径

    StorageFile storagefile = null;

    storagefile = await storagefolder .GetFileAsync(“Me.txt”);

    if (storagefile != null)

    {

        IRandomAccessStreamWithContentType readstream = await storageFile.OpenReadAsync();

        using (Stream stream = readstream.AsStreamForRead((int)readstream.Size))

        {

            byte[] content = new byte[stream.Length];

            await stream.ReadAsync(content, 0, (int)stream.Length);

            string readcontent = Encoding.UTF8.GetString(content, 0, content.Length);//文件内容读取

        }

    }

    else

    {

    //未找到文件

    }

}

以上就是简单的文件读取,不要忘记

using Windows.Storage;

using Windows.Storage.Streams;



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值