Common asp.net Code techniques (DPC&DWC Reference)--8

Source: Internet
Author: User
Tags constructor contains file system requires
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 are often handy to import the System.IO namespace to save unneeded Ty Ping (line 1).

Listing 2.2.1 uses the postback form technique we discussed in Chapter 1, "Common asp.net Page techniques." On line, a form with the runat= ' server ' is created. In the form, there are an asp:textbox control and a Submit button (Btnsubmit, line 77). When a user-visits the page, Page.IsPostBack is False with 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 is executed. On line 9, a DirectoryInfo object, Dirinfo, is created. Because the DirectoryInfo class is useful to retrieving information on a particular directory, including the files and Su Bdirectories of a particular directory, it isn ' t surprising that DirectoryInfo constructor, as a requires, t He path's 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 is times/you don ' t want to have to go through the steps of creating a instance of the Directoryin Fo class just to, say, delete a directory. The. NET Framework contains a Directory class for this purpose. This class cannot is instantiated and, instead, contains a number of static methods that can is used to work with any dire Ctory. We ' ll examine this class later in this section.


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

After we ' ve created a 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 a unsightly runtime error. To compensate for this, we use a Try ... Catch block, nesting the "calls to" DirectoryInfo classes properties and methods inside the Try block (lines through If the directory specified by the user doesn ' t exist, a DirectoryNotFoundException exception'll be thrown. The catch block starting on line would then Catch this exception and a error message would be displayed. Figure 2.8 shows the browser output when a user enters a nonexistent directory name.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.