【问题标题】:What is the URI to load text file in WP7?在 WP7 中加载文本文件的 URI 是什么?
【发布时间】:2011-12-03 16:41:36
【问题描述】:

使用相对 uri 路径在 WP7 中显示任何图像非常简单。 但是加载一个文本文件就成了一个大问号。

请查看图片,并尝试帮助将文件作为字符串变量的 URI 看起来如何。

        Dim S As String
        Dim U As New Uri("file:///Family_christmas;component/database/de/1.txt", UriKind.Absolute)
        Dim sr As New IO.StreamReader(U.LocalPath, System.Text.Encoding.Unicode)
        S = sr.ReadToEnd
        sr.Close()
        Me.Title = S.Split(Environment.NewLine)(0)
        Me.Text = S.Substring(Me.Title.Length + Environment.NewLine.Length)

* 以这种方式解决了 *

将文件声明为资源而不是内容。然后使用以下代码:

    Dim S As String
    Dim U As New Uri("database/de/1.txt", UriKind.Relative)
    Dim streamInfo As Windows.Resources.StreamResourceInfo = Application.GetResourceStream(U)
    Dim sr As New IO.StreamReader(streamInfo.Stream, System.Text.Encoding.Unicode)
    S = sr.ReadToEnd
    sr.Close()

【问题讨论】:

    标签: .net windows-phone-7 path uri


    【解决方案1】:

    默认情况下StreamReader 在文件系统中查找文件,而不是在资源中。您可以通过将您的文本文件标记为资源 并使用以下代码获取资源流:(抱歉 vb 到 c# 的转换:))

    StreamResourceInfo info = Application.GetResourceStream(new Uri("file:///Family_christmas;component/database/de/1.txt", UriKind.Relative));
    StreamReader reader = new StreamReader(info.Stream, System.Text.Encoding.Unicode);
    string text = reader.ReadToEnd();
    MessageBox.Show(text);
    

    这对我有用。

    【讨论】:

    • 非常感谢您的快速回复。你让我开心!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-16
    • 2011-11-19
    • 2018-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-04
    相关资源
    最近更新 更多