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

80酷酷网    80kuku.com

  asp.netFigure 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, aTeam2, and aTeam3 (lines 5, 6, and 7, respectively). These three ArrayLists are then populated with various strings in lines 12 through 22. Each of these ArrayLists is added to the htProjects HashTable on lines 26 through 28. As pointed out earlier, collection types can hold any Object, not just simple data types such as integers and strings.

On line 31, an instance of the IEnumerator interface is created and assigned to the enumerator for htProjects. (Each of the collection types contains a GetEnumerator() method that returns a read-only enumerator for the collection.) From lines 32 to 34, the enumProjects enumerator is stepped through, visiting each element in the Hashtable collection.

Note that each element returned to the enumerator from a Hashtable is an instance of the DictionaryEntry object. The DictionaryEntry object contains two public fields: Key and Value. Therefore, on line 33, to obtain the key of the current Hashtable element, we need to specify that we want the Key field of 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 & "
"
Loop
Because each entry in htProjects is an ArrayList collection itself, we need to create another enumerator to step through each element in each ArrayList. This is accomplished on line 37. At the end of our iteration through htProjects in lines 32 through 34, the enumerator enumProjects is positioned at the end of the collection. Because we are going to iterate through the htProjects collection again, we need to reposition the enumerator back to before the first element. This is accomplished with the Reset method of the IEnumerator interface (line 38).

In lines 39 through 48, the htProjects collection is enumerated through again. This time, each element of htProjects is also iterated through itself. On line 42, the enumTeam enumerator is assigned via the GetEnumerator() method of the current ArrayList collection. Next, the enumTeam enumerator is stepped through in lines 43 through 45, outputting each ArrayList element (line 44).

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


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

Note

All of the collection types we've examined in this chapter inherit from the ICollection interface. This interface is responsible for providing the size, enumeration, and synchronization methods for collection types. 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 the 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 from 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 was that the developer, without having to jump through hoops, could only read and write text files; reading and writing binary files with the FileSystemObject was 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 counterparts. In this section, we'll look at 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 FileSystemObject objects. The .NET Framework provides a plethora of file system–accessing classes in the System.IO namespace, including a DirectoryInfo class. This class will be examined in this section.

Listing 2.2.1 illustrates the DirectoryInfo class in action! From Listing2.2.1.aspx, the user can enter the name of a directory on the Web server. The page will then list the properties of that 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 the " & _
6:       "directory that 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
12:    ' Display the directory properties
13:    lblDirInfo.Text = "<b>Information for " & txtDirectoryName.Text & _
14:     "</b>
Attributes: " & _
15:     DisplayAttributes(dirInfo.Attributes) & "
Creation Time:" & _
16:     dirInfo.CreationTime.ToShortDateString() & _
17:     ", " & dirInfo.CreationTime.ToLongTimeString() & _
18:     "
Full Name: " & dirInfo.FullName & "
" & _
19:     "Root Drive: " & dirInfo.Root.Name & "
" & _
20:     "Parent Directory Name: " & dirInfo.Parent.Name & "
" & _
21:     "Directory Name: " & dirInfo.Name & "
Last Access Time: " & _
22:     dirInfo.LastAccessTime.ToShortDateString() & ", " & _
23:     dirInfo.LastAccessTime.ToLongTimeString() & "
" & _
24:     "Last Write Time: " & dirInfo.LastWriteTime.ToShortDateString() & _
25:     ", " & dirInfo.LastWriteTime.ToLongTimeString() & "
"
26:
27:    ' List all of the subdirectories for the current directory:
28:    lblSubDirectories.Text = "<b>Subdirectories of " & _
29:                dirInfo.FullName & "</b>
"
30:    Dim dirSubDirectory as DirectoryInfo
31:    For Each dirSubDirectory in dirInfo.GetDirectories()
32:     lblSubDirectories.Text &= dirSubDirectory.FullName & "
"
33:    Next
34:   Catch dnfException as DirectoryNotFoundException
35:    ' Whoops! A directoryNotFound Exception has been raised!
36:    ' 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
45: '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:
63:  ' 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
70: </script>
71:
72: <html>
73: <body>
74:  <form method="post" runat="server">
75:   <b>Get Information on Directory:</b>

76:   <asp:textbox runat="server" id="txtDirectoryName" /><p>
77:   <asp:button id="btnSubmit" runat="server" type="Submit" text="Go!" />
78:   <p><hr><p>
79:   <asp:label runat="server" id="lblDirInfo" /><p>
80:   <asp:label runat="server" id="lblSubDirectories" />
81:  </form>
82: </body>
83: </html>

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: