Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a Text File in Windows 8

I'm having some trouble reading an embedded resource (text file) in windows-8, 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'

like image 768
supergeek1982 Avatar asked Mar 10 '12 07:03

supergeek1982


People also ask

How do I read a text file in Windows?

You can open a TXT file with any text editor and most popular web browsers. In Windows, you can open a TXT file with Microsoft Notepad or Microsoft WordPad, both of which come included with Windows.

How do I read a text file?

To read from a text fileUse the ReadAllText method of the My. Computer. FileSystem object to read the contents of a text file into a string, supplying the path. The following example reads the contents of test.

How do I open a text file in reading?

Use the open() function with the 'r' mode to open a text file for reading. Use the read() , readline() , or readlines() method to read a text file. Always close a file after completing reading it using the close() method or the with statement.


3 Answers

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

like image 95
Chris Taylor Avatar answered Oct 23 '22 02:10

Chris Taylor


In AppStore libraries you can use following code:

Stream stream = this.GetType().GetTypeInfo().Assembly.GetManifestResourceStream(fileName);
like image 41
Pawel Lesnikowski Avatar answered Oct 23 '22 02:10

Pawel Lesnikowski


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

like image 37
Apoorv Avatar answered Oct 23 '22 03:10

Apoorv