簡體   English   中英

如何獲取GetResourceStream來讀取擴展名為.txt以外的文件?

[英]How do I get GetResourceStream to read files that have an extension other than .txt?

我有以下代碼:

 var resource = System.Windows.Application.GetResourceStream(new Uri("movies.txt", UriKind.Relative));
 StreamReader streamReader = new StreamReader(resource.Stream);
 string z = streamReader.ReadToEnd();

當我改變movies.txt擴展movies.plist ,代碼不再起作用,即使該文件仍然是純文本。 我該如何解決? 我需要將文件擴展名保持為.plist。

我得到的錯誤是第二行上的NullReferenceException

嘗試使用System.IO.FileInfo類打開文件。 這樣就可以打開FileStream。

var file = new System.IO.FileInfo("c:\foo.bar");
var FileStream = file.OpenRead();
Byte b = (Byte)FileStream.ReadByte();
Debug.WriteLine(b.ToString());

編輯:更正的代碼。

編輯:發布此消息后,我意識到此解決方案在Windows Phone環境中不可用。

如果您只想閱讀一個文本文件,那么使用它可能更有意義:

string result = System.IO.File.ReadAllText("myTextFile.ext");

您是否有特定原因要專門使用StreamReader?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM