How can I open a text file to the screen

  • Thread starter Thread starter Allen
  • Start date Start date
A

Allen

Can anybody tell me how to open (print)a text file to the screen. For
example, to click a botton and the file will be shown in the screen. Using
something like the code below.(the code below is not good, but I want to
give you an idea)

private: System::Void ShwCrdBtn_Click(System::Object^ sender,
System::EventArgs^ e)

{
FileInfo^ fi = gcnew FileInfo("C:\\MyFile.txt");
fi->OpenRead();

}
 
Basically, there are several things you can do depending on exactly what you
want the result to look like. If the only thing you want to be visible on
the page is the text from the text file, then I would create a page with
just a single span element with the runat="server", as follows:

<span id="spanTextFromFile" runat="server"></span>

and in the Load event, use the following code to assign the text from the
file to it:

Me.spanTextFromFile.InnerHtml=Server.HtmlEncode(System.IO.File.ReadAllText(Server.MapPath("mytextfile.txt")))

You can obviously use a control other than span, the code will be pretty
much the same. The reason for the Server.HtmlEncode() method is in case the
text file contains any special characters that could cause the page to not
render the way you want. That's basically all there is to it! It almost
seems a little too simple, but that's because it's a text file and we are
reading the entire thing, so we don't need to worry about stuff like opening
and closing the file, telling it from where to where in the file we want to
read, how to interpret the stuff in the file, etc.; all we really need to
tell it is to return all the text in this file as a string. Hopefully this
helps!
 
Can anybody tell me how to open (print)a text file to the screen.  For
example, to click a botton and the file will be shown in the screen.  Using
something like the code below.(the code below is not good, but I want to
give you an idea)

private: System::Void ShwCrdBtn_Click(System::Object^  sender,
System::EventArgs^  e)

{
FileInfo^ fi = gcnew FileInfo("C:\\MyFile.txt");
fi->OpenRead();

 }

in asp.net?
 
I appreciate your answer, but it sounds like XML to me.

--
Thanks
Allen
Nathan Sokalski said:
Basically, there are several things you can do depending on exactly what
you want the result to look like. If the only thing you want to be visible
on the page is the text from the text file, then I would create a page
with just a single span element with the runat="server", as follows:

<span id="spanTextFromFile" runat="server"></span>

and in the Load event, use the following code to assign the text from the
file to it:

Me.spanTextFromFile.InnerHtml=Server.HtmlEncode(System.IO.File.ReadAllText(Server.MapPath("mytextfile.txt")))

You can obviously use a control other than span, the code will be pretty
much the same. The reason for the Server.HtmlEncode() method is in case
the text file contains any special characters that could cause the page to
not render the way you want. That's basically all there is to it! It
almost seems a little too simple, but that's because it's a text file and
we are reading the entire thing, so we don't need to worry about stuff
like opening and closing the file, telling it from where to where in the
file we want to read, how to interpret the stuff in the file, etc.; all we
really need to tell it is to return all the text in this file as a string.
Hopefully this helps!
 
No, the ReadAllText method works for any text file, there is a separate
class for reading xml files (System.Xml.XmlReader). The ReadAllText method
returns every character in the text file exactly as it is in the file as its
String return value. For more details, take a look at the documentation. If
you are still having trouble figuring out exactly what to do, you may find
it useful to do a debug session, or if you would like me to help more, send
me a copy of your current code, the file you are reading, and the output
and/or error you are recieving. Good Luck!
--
Nathan Sokalski
(e-mail address removed)
http://www.nathansokalski.com/

Allen said:
I appreciate your answer, but it sounds like XML to me.
 
Back
Top