8

I'm having some trouble reading an embedded resource (text file) in , usually I use Assembly.GetExecutingAssembly() but I can't seem to do it in this one. I'm referencing the System.Reflection namespace but it says cannot find, thinking it was possibly removed.

Any ideas?

Currently using Windows 8 Consumer Preview

Code: Assembly readAssembly = Assembly.GetExecutingAssembly(); StreamReader streamReader = new StreamReader(readAssembly.GetManifestResourceStream("Test.txt"));

Error: System.Reflection.Assembly' does not contain a definition for 'GetExecutingAssembly'

supergeek1982
  • 113
  • 1
  • 2
  • 5
  • Possible duplicate of [How to read embedded resource text file][1] [1]: http://stackoverflow.com/questions/3314140/how-to-read-embedded-resource-text-file – Software Engineer Mar 10 '12 at 07:51
  • @supergeek, what's the problem with `GetExecutingAssembly()`? Does it return `null`? Does it throw an exception? Does it spit in your Cheerios? – Frédéric Hamidi Mar 10 '12 at 07:53
  • It says It can't find the reference though I'm definitely referencing System.Reflection.. it seems it might of been removed to work the way something like http://support.microsoft.com/kb/319292 does – supergeek1982 Mar 10 '12 at 07:56
  • Strange, [the docs](http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getexecutingassembly%28v=vs.110%29.aspx) say it's supported on Windows 8 Beta and Consumer Preview... Can you post the exact code that calls `GetExecutingAssembly()`? – Frédéric Hamidi Mar 10 '12 at 08:03
  • 1
    Do you mean WinRT? Windows 8 is unlikely to be the issue here... – Marc Gravell Mar 10 '12 at 08:11
  • @FrédéricHamidi I expect he means WinRT, not Windows 8 – Marc Gravell Mar 10 '12 at 08:11
  • @FrédéricHamidi updated the first section with code. I thought it was strange too because the docs mentioned it – supergeek1982 Mar 10 '12 at 08:39

3 Answers3

8

In WinRT the resources should be included in the package. You use Package.Current.InstalledLocation.GetFileAsync to read the resource.

The following post has some sample code:
http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/d4b327e3-a8f2-4d3c-8ed7-ba2ea953d0b9

ridoy
  • 6,274
  • 2
  • 29
  • 60
Chris Taylor
  • 52,623
  • 10
  • 78
  • 89
2

In AppStore libraries you can use following code:

Stream stream = this.GetType().GetTypeInfo().Assembly.GetManifestResourceStream(fileName);
Pawel Lesnikowski
  • 6,264
  • 4
  • 38
  • 42
  • Thanks, this is exactly what I needed. You can also find out the exact name to use for the file by seeing what's part of your assembly:(assuming you have the Assembly as shown above) string[] names = assembly.GetManifestResourceNames(); – Victor Chelaru Sep 13 '12 at 14:56
1

Why don't you use ReadFileAsync() Method available in VS 2012. Add the text file to your project and call the async method

Apoorv
  • 2,023
  • 1
  • 19
  • 42