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

Source: Internet
Author: User
Tags file system parent directory classic asp
asp.net Figure 2.6
Output of Listing 2.1.8 when viewed through a browser.

The code in Listing 2.1.8 begins by creating three ArrayList, Collections:ateam1, and ATeam2 (ATEAM3 5, 6, and 7, R espectively). These three arraylists are then populated and various strings in lines 22. All of these arraylists are added to the htprojects HashTable on lines 28. As pointed out earlier, collection types can hold any Object, not just simple data types such as integers and strings.

On line, a instance of the IEnumerator interface is created and assigned to the enumerator for htprojects. (Each of the collection types contains a GetEnumerator () method This returns a read-only for the enumerator.) From lines, the Enumprojects enumerator are stepped through, visiting each element in the Hashtable collection.

Note this each element returned to the enumerator from a Hashtable is a instance of the DictionaryEntry object. The DictionaryEntry object contains two public Fields:key and Value. Therefore, on line, to obtain the key of the current Hashtable element, we need to specify that we want the key field O f the current element. We could have created a DictionaryEntry instance and referenced the Key field in a more explicit manner as follows:

Dim Dictentry as DictionaryEntry
Do While Enumprojects.movenext ()
Dictentry = Enumprojects.current
Lblprojectlisting.text &= Dictentry.key & "<br>"
Loop
Because each entry into Htprojects is a ArrayList collection itself, we need to create another enumerator to step through E Ach element in each ArrayList. This is accomplished to line 37. At the "End of" our iteration through htprojects in lines through, the enumerator enumprojects was positioned at the en D of the collection. Because we are going to iterate through the Htprojects collection, "We again to need" reposition back to Enumerator Re the. This is accomplished with the Reset method of the IEnumerator interface (line 38).

In lines-through, the Htprojects collection is enumerated through. This time, each element of the htprojects is also iterated through itself. On line, the Enumteam enumerator are assigned via the GetEnumerator () method to the current ArrayList collection. Next, the Enumteam enumerator is stepped through in lines, through all outputting element (line 44).

Conclusion
The. NET Framework provides developers with a number of powerful collection-type classes, greatly extending the functional ity of the Scripting.Dictionary object, the sole collection type available for classic ASP developers. These collections are although each have the unique capabilities, are more alike than the they are. All of them share similar methods and properties, and can have their, elements iterated through using a number of technique S.


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

Note

All of the collection types we ' ve examined to chapter inherit from the ICollection interface. This interface is responsible to providing the size, enumeration, and synchronization methods for collection. All of the classes in the. NET Framework that inherit the ICollection interface support the basic collection functionality Discussed in this section, "Similarities among the Collection Types."


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

2. Working with the File System
Very often web application developers need to have the "ability to access" file system on the Web server. Perhaps they need to list the contents of a particular text file, remove a temporary directory or file, or copy a file fro M one location to another.

Classic ASP provided adequate support for working with the WEB server ' s file system. The FileSystemObject Object-along with its accompanying objects such as the File, Folder, and TextStream objects-permitted The classic ASP developer to perform rudimentary tasks with the Web server ' s file system. One serious shortcoming of the FileSystemObject is that developer, without has to jump through, hoops-only R EAD and write text files; Reading and writing binary files with the FileSystemObject is possible, but a pain.

The. NET Framework provides a number of classes for working with the file system. These classes are much more robust and have greater functionality, than their FileSystemObject. In this section, we'll look at the how to accomplish some common file system tasks:

Reading, creating, and deleting directories

Reading, writing, and creating files

Reading, creating, and deleting directories
In classic ASP, developers could access directory information with the Folder object, one of the many useful ECT objects. The. NET Framework provides a plethora of file system–accessing classes in the System.IO namespace, including a directoryi NFO class. This class is examined in the This section.

Listing 2.2.1 illustrates the DirectoryInfo class in action! From Listing2.2.1.aspx, the user can enter the name of directory on the WEB server. The page would then list the properties of this directory (if it exists), along with the directory ' s subdirectories. The output is shown in Figure 2.7.

Listing 2.2.1 The DirectoryInfo Class provides information about a particular Directory
1: <%@ Import namespace= "System.IO"%>
2: <script language= "VB" runat= "Server" >
3:sub Page_Load (sender as Object, E as EventArgs)
4:if not Page.IsPostBack Then
5:lbldirinfo.text = "Enter the fully qualified name of" & _
6: "Directory" you ' re interested in (<i>i.e., c:\</i>) "
7:else
8: ' A postback, so get the directory information
9:dim Dirinfo as DirectoryInfo = new
DirectoryInfo (Txtdirectoryname.text)
10:
11:try
: ' Display the directory properties
13:lbldirinfo.text = "<b>information for" & Txtdirectoryname.text & _
: "</b><br> Attributes:" & _
15:displayattributes (dirinfo.attributes) & "<br>creation Time:" & _
16:dirinfo.creationtime.toshortdatestring () & _
: "," & DirInfo.CreationTime.ToLongTimeString () & _
: "<br>full Name:" & Dirinfo.fullname & "<br>" & _
: "Root Drive:" & DirInfo.Root.Name & "<br>" & _
"Parent Directory Name:" & DirInfo.Parent.Name & "<br>" & _
"Directory Name:" & Dirinfo.name & "<br>last Access Time:" & _
22:dirinfo.lastaccesstime.toshortdatestring () & "," & _
23:dirinfo.lastaccesstime.tolongtimestring () & "<br>" & _
: "Last Write Time:" & DirInfo.LastWriteTime.ToShortDateString () & _
: "," & DirInfo.LastWriteTime.ToLongTimeString () & "<br>"
26:
"List all of the" subdirectories for "current directory:
28:lblsubdirectories.text = "<b>subdirectories of" & _
29:dirinfo.fullname & "</b><br>"
30:dim Dirsubdirectory as DirectoryInfo
31:for each dirsubdirectory in Dirinfo.getdirectories ()
32:lblsubdirectories.text &= dirsubdirectory.fullname & "<br>"
33:next
34:catch Dnfexception as DirectoryNotFoundException
: ' whoops! A Directorynotfound Exception has been raised!
"The user entered an invalid directory name!
37:lbldirinfo.text = "<font color=red><b>" & _
38:dnfexception.message & "</b></font>"
39:end Try
40:end If
41:end Sub
42:
43:
44:function displayattributes (FSA as fileattributes) as String
: ' Display the file attributes
46:dim stroutput as String = ""
47:
48:if (FSA bitand fileattributes.archive) > 0 Then
Stroutput &= "archived,"
49:if (FSA bitand fileattributes.compressed) > 0 Then
Stroutput &= "Compressed,"
50:if (FSA bitand fileattributes.directory) > 0 Then
Stroutput &= "Directory,"
51:if (FSA bitand fileattributes.encrypted) > 0 Then
Stroutput &= "Encrypted,"
52:if (FSA bitand Fileattributes.hidden) > 0 Then
Stroutput &= "Hidden,"
53:if (FSA bitand Fileattributes.normal) > 0 Then
Stroutput &= "Normal,"
54:if (FSA bitand fileattributes.notcontentindexed) > 0 Then _
55:stroutput &= "Not Content Indexed,"
56:if (FSA bitand Fileattributes.offline) > 0 Then
Stroutput &= "Offline,"
57:if (FSA bitand fileattributes.readonly) > 0 Then
Stroutput &= "Read only,"
58:if (FSA bitand Fileattributes.reparsepoint) > 0 Then
Stroutput &= "Reparse point,"
59:if (FSA bitand Fileattributes.sparsefile) > 0 Then
Stroutput &= "Sparse File,"
60:if (FSA bitand Fileattributes.system) > 0 Then
Stroutput &= "System,"
61:if (FSA bitand fileattributes.temporary) > 0 Then
Stroutput &= "Temporary,"
62:
"Whack off the trailing", "
64:if stroutput.length > 0 Then
65:displayattributes = stroutput.substring (0, Stroutput.length-2)
66:else
67:displayattributes = "No attributes found ..."
68:end If
69:end Function
: </script>
71:
: : <body>
Method=: <form "POST" runat= "Server" >
<b>get information on directory:</b><br>
runat=: <asp:textbox "Server" id= "Txtdirectoryname"/><p>
: <asp:button id= "btnsubmit" runat= "Server" type= "Submit" text= "go!"/>
: <p>: <asp:label runat= "Server" id= "Lbldirinfo"/><p>
: <asp:label runat= "Server" id= "Lblsubdirectories"/>
Bayi: </form>
: </body>
:

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.