繁体中文
设为首页
加入收藏
当前位置:.Net技术首页 >> Asp.Net开发 >> Common ASP.NET Code Techniques (DPC&DWC Reference)--7

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

2007-08-15 08:00:00  作者:  来源:互联网  浏览次数:0  文字大小:【】【】【
简介: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, aTeam2, and aTeam3 (lines 5, 6, and 7, respectiv...

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, 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:

71:

72:

73:

74:

75: Get Information on Directory:

76:

77:

78:


79:

80:

81:

82:

83:

责任编辑:admin
相关文章