Read Build Your Own ASP.NET 3.5 Website Using C# & VB Online
Authors: Cristian Darie,Zak Ruvalcaba,Wyatt Barnett
Tags: #C♯ (Computer program language), #Active server pages, #Programming Languages, #C#, #Web Page Design, #Computers, #Web site development, #internet programming, #General, #C? (Computer program language), #Internet, #Visual BASIC, #Microsoft Visual BASIC, #Application Development, #Microsoft .NET Framework
Visual Basic
LearningASP\VB\ReadFile_1.aspx
(excerpt)
<%@ Page Language="VB" %>
<%@ Import Namespace="System.IO" %>
Licensed to [email protected]
Working with Files and Email
605
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
As you can see, we’ve 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. Let’s add this method next. It will read the text from the text file and write
it out to the Label control:
Visual Basic
LearningASP\VB\ReadFile_2.aspx
(excerpt)
Licensed to [email protected]
606
Build Your Own ASP.NET 3.5 Web Site Using C# & VB
C#
LearningASP\CS\ReadFile_2.aspx
(excerpt)
We declare a new string variable named inputString to hold the text we’ll 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, we’re using the Using construct to ensure the stream object is disposed
of after we finish working with it:
Visual Basic
LearningASP\VB\ReadFile_2.aspx
(excerpt)
Using streamReader As StreamReader = _
File.OpenText(MapPath("myText.txt"))
C#
LearningASP\CS\ReadFile_2.aspx
(excerpt)
using (StreamReader streamReader =
File.OpenText(MapPath("myText.txt")))
{
Licensed to [email protected]
Working with Files and Email
607
Next, we call the ReadLine method of the streamReader object to get the first line
of the file:
Visual Basic
LearningASP\VB\ReadFile_2.aspx
(excerpt)
inputString = streamReader.ReadLine()
C#
LearningASP\CS\ReadFile_2.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
LearningASP\VB\ReadFile_2.aspx
(excerpt)
While (inputString <> Nothing)
resultLabel.Text &= inputString & "
"
inputString = streamReader.ReadLine()
End While
C#
LearningASP\CS\ReadFile_2.aspx
(excerpt)
while (inputString != null)
{
resultLabel.Text += inputString + "
";
inputString = streamReader.ReadLine();
}
Remember, While loops are used when you want to repeat the loop while a condition
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 we’ve reached the end of the file. Within the loop, we simply append the value
of inputString to the Label control’s 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
.
Licensed to [email protected]
608
Build Your Own ASP.NET 3.5 Web Site Using C# & VB
Figure 14.7. Reading a file using StreamReader
Accessing Directories and
Directory Information
Now that you have some understanding of writing to and reading from text files,
let’s look at accessing the directories in which those files are located. The classes
that are available in the System.IO namespace for working with directories and
directory information are as follows:
Directory
contains shared/static methods for creating, moving, and retrieving the contents
of directories
DirectoryInfo
contains instance methods for creating, moving, and retrieving the contents of
directories
Just like the File class, the Directory class contains shared/static methods, which
we can call without instantiating the class. The DirectoryInfo class, on the other
hand, requires instantiation, as it contains only instance methods. The Directory
class contains the following useful methods:
GetDirectories
returns a string array of directory names
GetFiles
returns a string array of filenames from a specific drive or directory
GetFileSystemEntries
returns a string array of directory and filenames
Licensed to [email protected]
Working with Files and Email
609
Let’s build an example page with a DropDownList control to display the directories
and files within the server’s
C:
drive. In the same
Learning
folder, create a web form named
Directories.aspx
, without a code-behind file, then add to it the code shown
here in bold:
Visual Basic
LearningASP\VB\Directories_1.aspx
(excerpt)
<%@ Page Language="VB" %>
<%@ Import Namespace="System.IO" %>
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
As you can see, our interface consists of a DropDownList control containing the
three choices from which the user can select (
Directories
,
Files
, or
Directories/Files
). When a user selects an item from the DropDownList control, the SelectedIndexChanged event is raised, and ViewDriveInfo is called. Licensed to [email protected]
610
Build Your Own ASP.NET 3.5 Web Site Using C# & VB
Now, let’s write the ViewDriveInfo method, which will write the specified information to the GridView control:
Visual Basic
LearningASP\VB\Directories_2.aspx
(excerpt)
C#
LearningASP\CS\Directories_2.aspx
(excerpt)
You might remember from
Chapter 3
that we use Select Case (VB) or switch (C#) statements to check for the possibility of multiple values of an object, rather than
Licensed to [email protected]
Working with Files and Email
611
just one. The Select Case or switch specifies the value that is to be checked (in
this case, the Text property of the selected list item):
Visual Basic
LearningASP\VB\Directories_2.aspx
(excerpt)
Select Case dirDropDown.SelectedItem.Text
C#
LearningASP\CS\Directories_2.aspx
(excerpt)
switch (dirDropDown.SelectedItem.Text)
Next, we use Case to specify the action to be performed for each significant value.
The data retrieved by the GetDirectories, GetFiles, or GetFileSystemEntries
method of Directory can be fed to the GridView as its DataSource. After we specify
the DataSource, we need to call the control’s DataBind method, as if we were
reading from a database, to fetch and display the data from the data source.
Save your work and test the results in your browser.
Figure 14.8
shows within the GridView the kind of results that display when the user selects an item from the
DropDownList.
Figure 14.8. Using the Directory class to view specific files, directories, or both, from a given drive Licensed to [email protected]