Working with Directories in Visual Basic

From Techotopia
Revision as of 18:57, 10 August 2007 by Neil (Talk | contribs) (New page: The previous chapter looked at file handing in Visual Basic. In this chapter we will the topic of directory handling in terms of opening, creating and listing the contents of directories u...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The previous chapter looked at file handing in Visual Basic. In this chapter we will the topic of directory handling in terms of opening, creating and listing the contents of directories using Visual Basic.

Creating and Removing a Directory In Visual Basic

Directory level activities in a Visual Basic application are performed by utilizing the DirectoryInfo class. One particularly useful method of DirectoryInfo the class is CreateSubdirectory() which, as the name suggests is used to create new subdirectories.

The DirectoryInfo class constructor takes a directory path as the sole parameter. In the following example a subdirectory named TempSubDir is created in the C:\Temp directory:

        Dim tmpDir As New IO.DirectoryInfo("C:\Temp")

        tmpDir.CreateSubdirectory("TempSubDir")

Another approach is to create a directory using the CreateDirectory() method. This approach does not require the creation of a DirectoryInfo object, and can be used with a full path:

My.Computer.FileSystem.CreateDirectory("C:\Temp\SubDir")

Similarly, a directory can be removed using the DeleteDirectory() method. The following example will only delete a directory if it is empty:

My.Computer.FileSystem.DeleteDirectory("C:\Temp\newdir", 
    FileIO.DeleteDirectoryOption.ThrowIfDirectoryNonEmpty)

The following code excerpt will delete the directory and any contents regardless of whether it contains files or not:

My.Computer.FileSystem.DeleteDirectory("C:\Temp\newdir", FileIO.DeleteDirectoryOption.DeleteAllContents)

The following example places the directory and contents into the Recycle Bin:

My.Computer.FileSystem.DeleteDirectory("C:\Temp\newdir", FileIO.RecycleOption.SendToRecycleBin)

Obtaining a Directory Information and Contents Listings in Visual Basic

Information about a directory and a listing of directory contents can be obtained using the Visual Basic DirectoryInfo class. The Name property of the DirectoryInfo provides the name of the directory, amd the FullName property provides the full path of the directory.

A full listing of the files in a directory can be obtained using the following Visual Basic code:

For Each fileName As String In 
My.Computer.FileSystem.GetFiles(My.Computer.FileSystem.SpecialDirectories.MyDocuments)
    listBox1.Items.Add(fileName)
Next

Similarly, a listing of the sub-directories contained in a directory can be obtained as follows:

        Dim tmpDir As New IO.DirectoryInfo("C:\Temp")

        Dim subDir As IO.DirectoryInfo

        For Each subDir In tmpDir.GetDirectories()
            ListBox1.Items.Add(subDir.Name)
        Next

Extracting Parts of a Path Name in Visual Basic

it is quite common to be passed a full path name by a Visual Basic method and to need only perhaps the file name or the directory path. Visual Basic provides easy access to each part of a path name as follows:

Dim tmpFile As System.IO.FileInfo
tmpFile = My.Computer.FileSystem.GetFileInfo("C:\Temp\test.txt")

Dim dirPath As String = testFile.DirectoryName ' Get just the Directory segment of the pathname
MsgBox(dirPath) ' Display it

Dim fileName As String = tmpFile.Name ' hget just the Filename part of the full pathname
MsgBox(fileName) ' Display it