【问题标题】:Using StreamReader in Windows Store app [duplicate]在 Windows 应用商店应用程序中使用 StreamReader [重复]
【发布时间】:2013-12-16 21:10:02
【问题描述】:

我发现了一个非常棘手的问题,我放弃了自己寻找答案。我目前正在开发一个 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();
}

【问题讨论】:

  • 哦,所以我对此知之甚少……好的,谢谢。但是这个问题比另一个问题更具体一些。
  • 嗯,我看到的链接答案的解决方案不再可用......
  • 我在包含新链接的答案上添加了评论。
  • 谢谢!我在这里找到了一些 MS 示例:msdn.microsoft.com/en-us/library/vstudio/jj155757.aspx

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


【解决方案1】:

我用它来读取资源文件:

    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 具有内容/请勿复制的构建操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-16
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多