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
File: WriteFile.aspx (excerpt)
<%@ Page Language="VB" %>
<%@ Import Namespace="System.IO" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Writing to Text Files</title>
<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" />
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:
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( _
End Using
End Sub
C# File: WriteFile.aspx (excerpt)
<script runat="server">
void WriteText(Object s, EventArgs e)
using (StreamWriter streamWriter = File.CreateText(
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
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
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.