在WP7中加载文本文件的URI是什么?

时间:2011-10-08 19:25:02

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

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

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

Uri error

        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)

*解决方法*

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

    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()

1 个答案:

答案 0 :(得分: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);

这对我有用。