January 19, 2002

Display a directory tree

Thanks to the GetDirectories and GetFiles methods of the System.IO.Directory class, you need very little code to iterate over all the directories and files of a directory tree. For example, the following code snippet prints the structure of a directory tree and (optionally) the name of files in each directory:

Filter file names on their names or attributes

The GetFiles and GetDirectories methods of the System.IO.Directory class can take an argument containing wildcards, to filter the result: ‘ Display all the *.txt files in C:DOCS.Dim fname As StringFor Each fname In Directory.GetFiles(“c:docs”, “*.txt”) Console.WriteLine(fname)Next You can use the GetCreationTime, GetLastAccessTime, GetLastWriteTime and GetAttributes shared methods to display information

Reading text files

In Visual Basic .NET you read a text file by first opening a StreamReader on it, and then iterating over all its lines until its Peek method returns -1: Dim sr As New StreamReader(“c:autoexec.bat”)’ Display all the text lines in the file.Do Until sr.Peek = -1 ‘ The ReadLine methods

Implementing ICloneable – Deep copies

The simplest way to create a generic deep copy routine, that is a procedure that can create a true, distinct copy of an object and all its dependent object, is to rely on the serialization features of the .NET framework. Function CloneObject(ByVal obj As Object) As Object ‘ Create a

Implementing IClonable – Shallow copies

An object that want to support cloning should implement the ICloneable interface. This interface exposes only one method, Clone, which returns a copy of the object. The ICloneable specifics don’t specify whether the object that the Clone method returns is a shallow copy or a deep copy. The difference is

Create directory paths

Visual Basic 6’s MkDir command, as well as the DOS MD command, can create only one subdirectory and fail if any subdirectory on the specified path doesn’t exist: ‘ This VB6 statement fails if C:MyApp doesn’t existMKDIR “c:MyAppMyDir” Conversely, the Directory.CreateDirectory method of Visual Basic .NET creates a directory, and