Build Your Own ASP.NET 3.5 Website Using C# & VB (105 page)

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

BOOK: Build Your Own ASP.NET 3.5 Website Using C# & VB
11.37Mb size Format: txt, pdf, ePub

612

Build Your Own ASP.NET 3.5 Web Site Using C# & VB

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 includes a utility class named Path that contains methods

for retrieving path information from files and directories. As an example, let’s build

a simple application that retrieves the directory and path information 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

LearningASP\VB\PathInfo_1.aspx

<%@ Page Language="VB" %>

<%@ Import Namespace="System.IO" %>

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">




<br/><b>Directory and Path Information<br/></b>










Licensed to [email protected]

Working with Files and Email

613

The page contains a simple Label control, which we’ll use to show all the directory

and path information. Next, let’s add the code that actually returns the path and

directory information:

Visual Basic

LearningASP\VB\PathInfo_2.aspx
(excerpt)


C#

LearningASP\CS\PathInfo_2.aspx
(excerpt)


Initially, we create a new string variable and set it equal to the full path of the text

file:

Licensed to [email protected]

614

Build Your Own ASP.NET 3.5 Web Site Using C# & VB

Visual Basic

LearningASP\VB\PathInfo_2.aspx
(excerpt)

Dim strPath As String

strPath = MapPath("myText.txt")

Next, we write into the Label control the complete file path, filename with extension,

directory, extension, and filename without extension, by using the Path class’s

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 However, those aren’t the only methods to which the Path class gives us access.

Here’s a list of all of the methods you can use:

ChangeExtension

modifies a file’s 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

Licensed to [email protected]

Working with Files and Email

615

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 server’s
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.

Uploading Files

There are many situations in which you’ll want your web application to allow users

to upload files to the server. For example, you could create a photo album site to

which users could upload images for others to view.

ASP.NET offers the FileUpload control for uploading files; it provides a text box

and
Browse
button to allow users to select files from their own computers and

transfer them to the server with ease. The FileUpload control can be found in the

Standard
tab of the T
oolbox; Figure 14.10 shows how it looks in V
isual Web Developer’s
Design
view. Licensed to [email protected]

616

Build Your Own ASP.NET 3.5 Web Site Using C# & VB

Figure 14.10. The FileUpload control in Visual Web Developer

The FileUpload control has the following read-only properties:

HasFile

is True if the user has uploaded a file, False otherwise

FileName

the name of the file as a string

FileContent

a stream that can be used to read the contents of the file

FileBytes

an array of bytes that can be used to read the contents of the file

PostedFile

an HttpPostedFile object for the uploaded file; this object has properties that

can be used to obtain additional data about the file, such as:

ContentLength

the file length in bytes

ContentType

the MIME type of the file (such as image/gif for a
.gif
file)1

The FileUpload control also has a method that you’ll find very useful: SaveAs. Although you can get the contents of an uploaded file using the FileContent and FileBytes properties described above, it’s usually more convenient to use the

SaveAs method to save files uploaded by users, but not before checking that HasFile

is True. The SaveAs method takes as a parameter a string containing the path and

the name of the target file.

1 View the complete list of MIME types at http://www.w3schools.com/media/media_mimeref.asp. Note that there’s no guarantee the MIME type is correct, as it’s easily manipulated by the client. Licensed to [email protected]

Working with Files and Email

617

Let’s test this control out. Create a new web form named
FileUpload.aspx
in the

LearningASP\VB
or
LearningASP\CS
folder, and populate it with this code:

Visual Basic

LearningASP\VB\FileUpload.aspx
(excerpt)

<%@ Page Language="VB" %>

<%@ Import Namespace="System.IO" %>

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">




<br/><b>File Upload<br/></b>






OnClick="UploadFile" />








If you’re using C#, you should place the following code in the

runat="server"> tag:

Licensed to [email protected]

Other books

Miss Grief and Other Stories by Constance Fenimore Woolson
Crossroads by Wendy Saunders
Exile's Challenge by Angus Wells
Ancillary Justice by Ann Leckie
Death Among Us by Jack Crosby
The Capture by Tom Isbell
Dead Ends by Erin Jade Lange