2011-10-08 37 views
2

在WP7中使用相對uri路徑顯示任何圖像非常簡單。 但加載文本文件成爲一個大問號。在WP7中加載文本文件的URI是什麼?

請看看圖片,並嘗試幫助如何看起來像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) 

*解決了WAY *

申報文件的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

默認情況下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); 

這爲我工作。

+1

非常感謝您的快速回復。你讓我今天一整天都感覺很好! – Nasenbaer

相關問題