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

发布于 2024-12-08 23:58:14 字数 956 浏览 0 评论 0原文

使用相对 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)

* 以此方式解决*

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

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

It so simple to show any image in WP7 using relative uri path.
But loading a text file becomes a big questionmark.

Please look at the image and please try to help how the URI should looks like to have the file as string variable.

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)

* SOLVED THAT WAY *

Declare file as ressource not as content. Then use the following code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤城病女 2024-12-15 23:58:14

默认情况下,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);

这对我有用。

By default StreamReader looks for the file in the file system, not in resources. You can get a resource stream my marking your text file as Resource and using this code: (sorry for the vb to c# conversion :))

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

This worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文