I'm trying to include a text file that contains some static data that I need to read in when the app starts up. I've added the file and marked the Build Action to "Resource" but I'm unsure of how to actually read it in as a stream. Anyone know how to do this?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You can use the System.Windows.Application.GetResourceStream
method:
var resource = System.Windows.Application.GetResourceStream(
new Uri("textfile.txt",UriKind.Relative));
should do the trick
回答2:
try this:
var resource = Application.GetResourceStream(
new Uri(@"/YOURASSEMBLYNAME;component/Stations.txt",
UriKind.Relative));
StreamReader streamReader = new StreamReader(resource.Stream);
string x = streamReader.ReadToEnd();
That should work for you.