Working with Directories in Visual Basic
Previous | Table of Contents | Next |
Working with Files in Visual Basic | Drawing Graphics in Visual Basic |
The previous chapter looked at file handing in Visual Basic. In this chapter we will cover 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 the DirectoryInfo 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
<google>ADSDAQBOX_FLOW</google> 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, and 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 ' Get just the Filename part of the full pathname MsgBox(fileName) ' Display it
Copying Directories in Visual Basic
A directory and all of the files therein may be copied to a new folder using the CopyDirectory() method:
My.Computer.FileSystem.CopyDirectory("C:\Temp", "C:\Temp2", True)
The True parameter tells Visual Basic to overwrite the target directory if it already exists. Setting this to False will prevent existing directories from being overwritten and instead, the contents of the two directories will be merged.
Renaming a Directory in Visual Basic
To rename a directory using Visual Basic:
My.Computer.FileSystem.MoveDirectory("C:\Temp", "C:\Temp2")
The following code excerpt will overwrite the target directory if it already exists:
My.Computer.FileSystem.MoveDirectory("C:\Temp", "C:\Temp2", True)