Metro下读取txt文件

情况1:txt是Utf8格式的。

读取代码:IList<String> lines = await Windows.Storage.FileIO.ReadLinesAsync(file);//正常读取

 

情况2:txt不是utf8格式的,比如GB2312格式。

using (var stream = await file.OpenAsync(FileAccessMode.Read))
{

    Encoding encoding = Encoding.GetEncoding("GB2312");

    StreamReader reader = new StreamReader(stream.AsStream(), encoding);

    char[] chars = new char[stream.Size];      

    reader.ReadAsync(chars, 0, chars.Length); 

    string text= new string(chars);//转成string

}

这个必须知道文本的格式,不然的话没办法。Metro里面不知道怎么读取文本格式,有知道请留言。

posted @ 2015-04-01 10:47  wangjinming  阅读(300)  评论(0编辑  收藏  举报