Writing Content to a Text File
For the purposes of the next few exercises, lets work again with our old friend,
the Learning web application. Start Visual Web Developer, go to File > Open
Web Site, and open the Learning application.
Right-click the project in Solution Explorer, and select Add New Item. Select the
Web Form template, name it WriteFile.aspx, and make sure you arent using a
code-behind file or a master page. Click Add, then enter the code shown here in
bold:
File: WriteFile.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>Writing to Text Files</title>
</head>
<body>
<form id="form1" runat="server">
Write the following text within a text file:<br />
<asp:TextBox ID="myText" runat="server" />
<asp:Button ID="writeButton" Text="Write" runat="server"
OnClick="WriteText" />
</form>
</body>
</html>
As you can see, we import the System.IO namespacethe namespace that contains
the classes for working with text filesfirst. Next, we add a TextBox control to
handle collection of the user-entered text, and a Button control to send the infor-
mation to the server for processing.
Next, in the <head> tag, well create the WriteText method mentioned in the
OnClick attribute of the Button. This method will write the contents of the
TextBox to the text file:
576
Chapter 14: Working with Files and Email
Visual Basic File: WriteFile.aspx (excerpt)
<script runat="server">
Sub WriteText(ByVal s As Object, ByVal e As EventArgs)
Using streamWriter As StreamWriter = File.CreateText( _
"C:\WebDocs\Learning\myText.txt")
streamWriter.WriteLine(myText.Text)
End Using
End Sub
</script>
C# File: WriteFile.aspx (excerpt)
<script runat="server">
void WriteText(Object s, EventArgs e)
{
using (StreamWriter streamWriter = File.CreateText(
@"C:\WebDocs\Learning\myText.txt"))
{
streamWriter.WriteLine(myText.Text);
}
}
</script>
Apart from the new Using construct, the code is pretty straightforward. First, we
create a StreamWriter variable called streamWriter. To obtain the variables
value, we call the CreateText method of the File class, passing in the location
of the text file, which returns a new StreamWriter. Dont forget that C# needs
to escape backslashes when theyre used in strings, so the path to our file must
use \\ to separate folder names, or use the @ character in front of the string so
that backslashes are automatically ignored.
What about Using, then? Similarly to database connections, streams are some-
thing that we need to close when were done working with them, so they dont
occupy resources unnecessarily. The Using construct is a common means of en-
suring that the stream is closed and disposed of after we work with it.
Disposing of Objects
Technically, when we work with Using, the object is disposed of, rather
than simply closed. The action is identical to explicitly calling its Dispose
method.
When the code enclosed by Using finishes executing, streamWriters
Dispose method is called automatically for you. This ensures that it doesn't
keep any resources locked, and that the object itself is removed from memory
immediately.
577
Writing Content to a Text File

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.