C# File: WriteFile.aspx (excerpt)
using (StreamWriter streamWriter = File.AppendText(
MapPath("myText.txt")))
The MapPath method returns the full path to the filename that you pass in as a
parameter, and can make for cleaner code thats easier to read.
Reading Content from a Text File
Just as you used the CreateText and AppendText methods of the File class to
return a new StreamWriter object, you can use the OpenText method of the File
class to return a new StreamReader. Once the StreamReader has been established,
you can loop through the text file using a While loop in conjunction with the
objects ReadLine method, to examine the contents of the text file.
To experiment with the process of reading from text files, create a new web form
named ReadFile.aspx in the same way that you created WriteFile.aspx, and
add this code to it:
Visual Basic File: ReadFile.aspx (excerpt)
<%@ Page Language="VB" %>
<%@ Import Namespace="System.IO" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Reading from Text Files</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="readButton" Text="Read" runat="server"
OnClick="ReadText" />
<br />
<asp:Label ID="resultLabel" runat="server" />
</form>
</body>
</html>
As you can see, weve simply added a Button and Label to the page. When the
user clicks the button, the Click event will be raised and the ReadText method
will be called. Lets add this method next. It will read the text from the text file
and write it out to the Label control:
580
Chapter 14: Working with Files and Email
Visual Basic File: ReadFile.aspx (excerpt)
<script runat="server">
Sub ReadText(ByVal s As Object, ByVal e As EventArgs)
Dim inputString As String
resultLabel.Text = ""
Using streamReader As StreamReader = _
File.OpenText(MapPath("myText.txt"))
inputString = streamReader.ReadLine()
While (inputString <> Nothing)
resultLabel.Text &= inputString & "<br />"
inputString = streamReader.ReadLine()
End While
End Using
End Sub
</script>
C# File: ReadFile.aspx (excerpt)
<script runat="server">
void ReadText(Object s, EventArgs e)
{
string inputString;
resultLabel.Text = "";
using (StreamReader streamReader =
File.OpenText(MapPath("myText.txt")))
{
inputString = streamReader.ReadLine();
while (inputString != null)
{
resultLabel.Text += inputString + "<br />";
inputString = streamReader.ReadLine();
}
}
}
</script>
We declare a new string variable named inputString to hold the text well read
from the text file. Next, we set the text value of the Label control to an empty
string. We do this in case the user presses the Read button when the Label already
contains text from a previous click.
The next thing our method has to do is call the OpenText method of the File
class to return a new StreamReader, again passing in the full path to the text file.
And, once again, were using the Using construct to ensure the stream object is
disposed of after we finish working with it.
581
Reading Content from a Text File
Visual Basic File: ReadFile.aspx (excerpt)
Using streamReader As StreamReader = _
File.OpenText(MapPath("myText.txt"))
C# File: ReadFile.aspx (excerpt)
using (StreamReader streamReader =
File.OpenText(MapPath("myText.txt")))
{
Next, we call the ReadLine method of the streamReader object to get the first
line of the file:
Visual Basic File: ReadFile.aspx (excerpt)
inputString = streamReader.ReadLine()
C# File: ReadFile.aspx (excerpt)
inputString = streamReader.ReadLine();
Now we loop through the file, reading each line and adding it, in turn, to the end
of the text in the Label:
Visual Basic File: ReadFile.aspx (excerpt)
While (inputString <> Nothing)
resultLabel.Text &= inputString & "<br />"
inputString = streamReader.ReadLine()
End While
C# File: ReadFile.aspx (excerpt)
while (inputString != null)
{
resultLabel.Text += inputString + "<br />";
inputString = streamReader.ReadLine();
}
Remember, While loops are used when you want to repeat the loop while a con-
dition remains True. In this case, we want to loop through the file, reading in
lines from it until the ReadLine method returns the value Nothing (null in C#),
which indicates that weve reached the end of the file. Within the loop, we simply
append the value of inputString to the Label controls Text property using the
&= operator (+= in C#), then read the next line from streamReader into
inputString.
Save your work and test the results in the browser. Figure 14.7 shows the contents
of the text file, as displayed by ReadFile.aspx.
582
Chapter 14: Working with Files and Email

Get Build Your Own ASP.NET 2.0 Web Site Using C# & VB, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.