Changes

Jump to: navigation, search

Working with Directories in Visual Basic

3,365 bytes added, 18:57, 10 August 2007
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...
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:

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

tmpDir.CreateSubdirectory("TempSubDir")
</pre>

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:

<pre>
My.Computer.FileSystem.CreateDirectory("C:\Temp\SubDir")
</pre>

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

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

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

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

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

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

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

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

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

<pre>
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
</pre>

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

<pre>
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
</pre>

Navigation menu