KnowDotNet Visual Organizer

Using the System.IO Namespace - The Directory Class

by Les Smith
Print this Article Discuss in Forums

The System.IO Namespace is one of the greatest time-savers in the .NET Framework.  Not only is it a big time-saver, but it removes a lot of the nitty-gritty "bit-fiddling" involved with working with files, filenames, paths, etc.  This is the second in a series of articles exploring the System.IO Namespace.  In this article, I will explore part of the Directory Class.  It is much larger than the Path Class, so I will not cover all of its methods.

First, I want to build an array of files of a certain type from a directory.  In VB6, I would have create and constantly ReDim the array as I found more files in the Dir loop.  Notice that the .NET Directory.GetFiles method will create and fill the array with one line of code.

      ' now get a array of all of the .txt files
      ' in a directory in VB6
      Dim file As String
      Dim files() As String
      file = Dir("c:\*.txt")
      
Dim cnt As Integer = 0
      
Do While Len(file) > 0
        
If cnt > 0 Then
            ReDim Preserve files(cnt)
        
End If
         files(0) = file
         cnt = cnt + 1
      
Loop

      ' .NET way
      files = Directory.GetFiles("c:\*.txt")

A similar functionality exists with respect to getting an array of all of the directories on a drive or sub directories under a directory.  In VB6, you would have to have set up a Dir loop to retrieve all of the possible sub directory paths.  In .NET, you can build the array with one line of code as follows.

      Dim dirs As String() = Directory.GetDirectories("c:\")

Possibly not as useful, but I can fill an array with all of the files and sub directory names in a directory with one line of code shown below.

      Dim FilesAndDirs As String() = Directory.GetFileSystemEntries("c:\")

I can create an array of all of the drives on my computer with the following line of code.

      Dim Drives As String() = Directory.GetLogicalDrives()

If I wanted to create a path with a number of directories in VB6, it was not real fun.  I will not reproduce the code here, but I would have to check for the presence of each directory, and if it was not present, I would have to add that directory before I could add any sub directories.  Depending on the length of path, i.e., the number of sub directories, this code could be extensive, even if you figured a way to create loop.  But in .NET, the Directory.CreateDirectory method will create any path, on an existing drive, and you do not have to worry about which if any of the directories already exist.  The CreateDirectory method creates the directories that it needs to create in order to satisify your request.  The following line of code does the job.

      Directory.CreateDirectory("c:\test\test1\test2")

The Directory.Move method will move either a file or directory.  In VB6, I could have moved a file easy enough, but moving a directory without doing it the hard way, by creating the directory and moving all of the files, I believe I would have had to used the Shell System object.  Documentation was scarce on the use of that object, although I was able to find a book on the subject.  In .NET, again, it can be done with a Framework method, Directory.Move.

      Directory.Move("c:\test", "h:\test")

This may be kind of sneaky, but you can set the creation time, last write time, and time of last access of a file or directory using these Directory methods.

      Directory.SetCreationTime("c:\test\file.txt", Now)
      Directory.SetLastWriteTime("c:\test\file.txt", Now)
      Directory.SetLastAccessTime("c:\test\file.txt", Now)

I have not covered all of the nuances of the IO.Directory Class, but I have given you enough to show you the power of the class.  At the same time, you may have done something I have; learned something else about the framework.

Top of Page

Writing Add-Ins for Visual Studio .NET
Writing Add-ins for Visual Studio .NET
by Les Smith
Apress Publishing