Common ASP.NET Code Techniques (DPC&DWC Reference)--8

asp.net

Figure 2.7
Output of Listing 2.2.1 when viewed through a browser.

When working with the various file system classes, it is often handy to import the System.IO namespace to save unneeded typing (line 1).

Listing 2.2.1 uses the postback form technique we discussed in Chapter 1, "Common ASP.NET Page Techniques." On line 74, a form with the runat="server" attribute is created. In the form, there is an asp:textbox control and a submit button (btnSubmit, line 77). When a user first visits the page, Page.IsPostBack is False and lines 5 and 6 in the Page_Load event handler are executed, displaying an instructional message.

After the user enters a directory name and submits the form, the Page.IsPostBack property is set to True and the code from lines 8 through 39 is executed. On line 9, a DirectoryInfo object, dirInfo, is created. Because the DirectoryInfo class is useful for retrieving information on a particular directory, including the files and subdirectories of a particular directory, it isn't surprising that the DirectoryInfo constructor requires, as a parameter, the path of the directory with which the developer is interested in working. In this case, we are interested in the directory specified by the user in the txtDirectoryName text box.

--------------------------------------------------------------------------------

Note

The DirectoryInfo class represents a specific directory on the Web server's file system; the DirectoryInfo constructor requires that you specify a valid directory path. However, there may be times when you don't want to have to go through the steps of creating an instance of the DirectoryInfo class just to, say, delete a directory. The .NET Framework contains a Directory class for this purpose. This class cannot be instantiated and, instead, contains a number of static methods that can be used to work with any directory. We'll examine this class later in this section.

--------------------------------------------------------------------------------

After we've created an instance of the DirectoryInfo class, we can access its methods and properties. However, what if the user specified a directory that does not exist? Such a case would generate an unsightly runtime error. To compensate for this, we use a Try ... Catch block, nesting the calls to the DirectoryInfo classes properties and methods inside the Try block (lines 13 through 33). If the directory specified by the user doesn't exist, a DirectoryNotFoundException exception will be thrown. The Catch block starting on line 34 will then catch this exception and an error message will be displayed. Figure 2.8 shows the browser output when a user enters a nonexistent directory name.

时间: 2024-10-30 00:57:36

Common ASP.NET Code Techniques (DPC&DWC Reference)--8的相关文章

Common ASP.NET Code Techniques (DPC&DWC Reference)--7

asp.net Figure 2.6Output of Listing 2.1.8 when viewed through a browser. The code in Listing 2.1.8 begins by creating three ArrayList collections: aTeam1, aTeam2, and aTeam3 (lines 5, 6, and 7, respectively). These three ArrayLists are then populated

Common ASP.NET Code Techniques (DPC&DWC Reference)--6

asp.net Figure 2.5Output of Listing 2.1.5 when viewed through a browser. If you've worked with classic ASP, you are likely familiar with the concept of session-level variables. These variables are defined on a per-user basis and last for the duration

Common ASP.NET Code Techniques (DPC&dwc Reference)--2

asp.net Figure 2.1Output of Listing 2.1.1 when viewed through a browser. Adding Elements to an ArrayListIn Listing 2.1.1 we create two ArrayList class instances, aTerritories and aStates, on lines 5 and 6, respectively. We then populate the aStates A

Common ASP.NET Code Techniques (DPC&DWC Reference)--9

asp.net Figure 2.8An attractive error message is displayed if the user enters an invalid directory name. A useful property of the DirectoryInfo class (and the FileInfo class, which we'll examine in the next section, "Reading, Writing, and Creating Fi

Common ASP.NET Code Techniques (DPC&DWC Reference)--4

asp.net Figure 2.3Output of Listing 2.1.3 when viewed through a browser. Listing 2.1.3 begins with the instantiation of the SortedList class (line 4). slTestScores, the SortedList instance, contains the test scores from five students (see lines 7 thr

Common ASP.NET Code Techniques (DPC&DWC Reference)--5

asp.net Figure 2.4Output of Listing 2.1.4 when viewed through a browser. Adding Elements to a QueueIn Listing 2.1.4, we begin by creating an instance of the Queue class, qTasks (line 5). In line 7 through 14, we add eight new elements to qTasks using

Common ASP.NET Code Techniques (DPC&DWC Reference)--3

asp.net Figure 2.2Output of Listing 2.1.2 when viewed through a browser. Adding Elements to a HashtableIn Listing 2.1.2, we begin by creating an instance of the Hashtable class, htSalaries, on line 5. Next, we populate this hash table with our variou

Common ASP.NET Code Techniques (DPC&dwc Reference)

Figure 2.1Output of Listing 2.1.1 when viewed through a browser. Adding Elements to an ArrayListIn Listing 2.1.1 we create two ArrayList class instances, aTerritories and aStates, on lines 5 and 6, respectively. We then populate the aStates ArrayList

Common ASP.NET Code Techniques (DPC&DWCReference)-

One of the things that I personally found frustrating with classic ASP was the difficulty associated with completing many common Web-related tasks. For example, the need to allow Web visitors to upload files to the Web server is fairly common for Web