I want to be able to read any file into a string, for instance the way notepad might open a word file. Using the following code:
StreamReader sr = new StreamReader(filePath);
text += sr.ReadToEnd();
sr.Close();
works fine on a basic text file but when using it on say a word file I just get a few odd characters whereas opening the same file in notepad shows me the entire file, text, special characters etc. I’m using this as part of a file drop into a textbox. Basically I’m looking to get the same output you would get when you open any file in notepad. What should I be using instead?
Using your code from the original question and opening a file, does show the entire stream (when looking it in debugger) – The problem is that most of these binary files have null terminators (
\0char) which will cause most viewers to stop reading the contents of the stream.If you remove/escape the
'\0'you’ll see the entire stream just like in notepad.For example:
Add a textbox1 to a form and see for yourself… You’ll see the entire stream…