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 specific drive
More Options
The GetDirectories, GetFiles, and GetFileSystemEntries methods
accept more than simple drive or directory names. For instance, if you wanted
to view only text files, you could use the following VB code:
Directory.GetFiles("C:\", "*.txt")
In this example, the GetFiles method would retrieve from the root of the
C: drive all files that have the .txt extension.
Working with Directory and File Paths
The System.IO namespace also includes a utility class named Path that contains
methods for retrieving path information from files and directories. As an example,
lets build a simple application that retrieves the directory and path information
586
Chapter 14: Working with Files and Email
for a text file. Create a new web form named PathInfo.aspx in the Learning
directory, then add to it the code shown here in bold:
Visual Basic File: PathInfo.aspx
<%@ 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>Directory and Path Information</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="resultLabel" runat="server" />
</form>
</body>
</html>
The page contains a simple Label control, which well use to show all the directory
and path information. Next, lets add the code that actually returns the path and
directory information:
Visual Basic File: PathInfo.aspx (excerpt)
<script runat="server">
Sub Page_Load(ByVal s As Object, ByVal e As EventArgs)
Dim strPath As String
strPath = MapPath("myText.txt")
resultLabel.Text &= "File Path: " & strPath & "<br />"
resultLabel.Text &= "File name: " & _
Path.GetFileName(strPath) & "<br />"
resultLabel.Text &= "Directory: " & _
Path.GetDirectoryName(strPath) & "<br />"
resultLabel.Text &= "Extension: " & _
Path.GetExtension(strPath) & "<br />"
resultLabel.Text &= "Name without Extension: " & _
Path.GetFileNameWithoutExtension(strPath)
End Sub
</script>
C# File: PathInfo.aspx (excerpt)
<script runat="server">
void Page_Load(Object s, EventArgs e)
587
Working with Directory and File Paths
{
string strPath;
strPath = MapPath("myText.txt");
resultLabel.Text += "File Path: " + strPath + "<br />";
resultLabel.Text += "File name: " +
Path.GetFileName(strPath) + "<br />";
resultLabel.Text += "Directory: " +
Path.GetDirectoryName(strPath) + "<br />";
resultLabel.Text += "Extension: " +
Path.GetExtension(strPath) + "<br />";
resultLabel.Text += "Name w/out Extension: " +
Path.GetFileNameWithoutExtension(strPath);
}
</script>
Initially, we create a new string variable and set it equal to the full path of the
text file:
Visual Basic File: PathInfo.aspx (excerpt)
Dim strPath As String
strPath = MapPath("myText.txt")
Next, we write into the Label control the complete file path, filename with ex-
tension, directory, extension, and filename without extension, by using the Path
classs GetFileName, GetDirectoryName, GetExtension, and
GetFileNameWithoutExtension methods, respectively.
Save your work and test the results in your browser. Figure 14.9 shows how all
the information for the text file is displayed.
Figure 14.9. Retrieving the path, filename, directory, file
extension, and filename without extension for the text file
588
Chapter 14: Working with Files and Email
However, those arent the only methods to which the Path class gives us access.
Heres a list of all of the methods you can use:
ChangeExtension
modifies a files extension
Combine
joins two file paths
GetDirectoryName
returns the directory part of a complete file path
GetExtension
returns the file extension from a file path
GetFileName
returns the filename from a file path
GetFileNameWithoutExtension
returns the filename without the file extension from a file path
GetFullPath
expands the supplied file path with a fully qualified file path
GetPathRoot
returns the root of the current path
GetTempFileName
creates a uniquely named file and returns the name of the new file
GetTempPath
returns the path to the servers temp directory
HasExtension
returns True when a file path contains a file extension
IsPathRooted
returns True when a file path makes reference to a root directory or network
share
See the .NET Framework SDK documentation for full details on all of these
methods.
589
Working with Directory and File Paths

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.