在Windows应用商店应用中使用StreamReader

时间:2013-12-16 21:10:02

标签: c# windows-8 windows-store-apps streamreader

我发现了一个非常难以解决的问题,我放弃了自己找到答案。我目前正在开发 Windows 8商店应用,我正在使用MS Visual Studio 2012(当然)。

我想在应用程序启动时读取csv文件。我创建了一个带有类的新.cs文件,其中一个应该拥有这个文件读取方法。在这一点上一切都很好,但是当我开始实现阅读器时,我收到以下错误消息:

  

最佳重载方法匹配System.IO.StreamReader.StreamReader(System.IO.Stream)'有一些无效的论点。   参数1:无法转换为' string'到' System.IO.Stream'

我很想知道,因为所有的MS引用都说System.IO.StreamReader可以有一个字符串作为参数。奇怪的。为了好玩,我创建了一个简单的控制台应用程序,我复制了所有这些StreamReader的东西,这是一个奇迹,它正常工作......

也许我对Win 8 Store应用程序知之甚少,但老实说我找不到有关此问题的任何信息。

商店应用程序的代码:

public void readCSV()
{
    string path = @"ms-appdata://Asstes/Content/data.csv";
    try
    {
        using (System.IO.StreamReader sr = newSystem.IO.StreamReader(path)) 
        {

            while (sr.Peek() >= 0)
            {
                Console.WriteLine(sr.ReadLine());
            }
        }
    }
    finally
    {

    }

}

编译器接受的代码(作为控制台应用程序):

static void Main(string[] args)
{

    System.IO.StreamReader reader;
    reader = new System.IO.StreamReader("path.csv");
    reader.ReadLine();
}

1 个答案:

答案 0 :(得分:0)

我用它来读取资源文件:

    public static async Task<IList<Person>> GetPeople()
    {
        var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/People.xml"));
        var stream = await file.OpenStreamForReadAsync();
        using (StreamReader sr = new StreamReader(stream))
        {
            while (sr.Peek() >= 0)
            {
                var a = sr.ReadLine();
            }
        }
     }

People.xml具有Content / Do copy的构建操作。

相关问题