在Windows RT应用程序中从文件读取 [英] Reading from file in a Windows RT Application

查看:70
本文介绍了在Windows RT应用程序中从文件读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何逐行读取Windows中的文本文件RT?

Possible Duplicate:
How to read a text file line by line Windows RT?

我正在尝试在C#中逐行读取文件.

I am trying to read from file line by line in C#.

这是我的代码

   String filename = "apoel.txt";

   System.IO.StreamReader file = new System.IO.StreamReader(filename);

我按照并严格按照它们进行操作.问题是我不断收到错误消息

I followed instructions from an MSDN page and followed them exactly. The problem is I keep getting the errors

System.IO.StreamReader.StreamReader(System.IO.Stream)的最佳重载方法匹配有一些 无效的参数
参数1:无法从字符串"转换为"System.IO.Stream"

The best overloaded method match for System.IO.StreamReader.StreamReader (System.IO.Stream)' has some invalid arguments
Argument 1: cannot convert from 'string' to 'System.IO.Stream'

我添加了using System.IO;在我的代码顶部

I added using System.IO; on the top of my code

我做错了什么?如果有帮助,这是Windows Metro应用程序

What am I doing wrong? If it is of any help this is a Windows Metro app

还可以有人向我解释为什么我发布的MSDN中的文章是错误的并且无法正常工作吗?请不要给我替代方法.请告诉我为什么我的代码在MSDN中这样解释时不起作用

Also can someone explain to me why the article from MSDN that I post is wrong and not working? Do not give me an alternative please. Please tell me why my code is not working while it is explained like that in MSDN

推荐答案

这是我在Windows 8中用于读取/写入文件的代码.它可以正常工作,希望对您有所帮助.

Here is the code i use for Reading/Writing a file in Windows 8. It works and i hope it helps you too.

private StorageFolder localFolder;
// Read from a file line by line
public async Task ReadFile()
{
    try
    {
        // get the file
        StorageFile myStorageFile = await localFolder.GetFileAsync("MyDocument.txt");
        var readThis = await FileIO.ReadLinesAsync(myStorageFile);
        foreach (var line in readThis)
        {
            String myStringLine = line;
        }
        Debug.WriteLine("File read successfully.");
    }
    catch(FileNotFoundException ex)
    {   
        Debug.WriteLine(ex);           
    }
}
// Write to a file line by line
public async void SaveFile()
{
    try
    {
        // set storage file
        StorageFile myStorageFile = await localFolder.CreateFileAsync("MyDocument.txt", CreationCollisionOption.ReplaceExisting);
        List<String> myDataLineList = new List<string>();
        await FileIO.WriteLinesAsync(myStorageFile, myDataLineList);
        Debug.WriteLine("File saved successfully.");
    }
    catch(FileNotFoundException ex)
    {  
        Debug.WriteLine(ex);            
    }
}

这篇关于在Windows RT应用程序中从文件读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆