Académique Documents
Professionnel Documents
Culture Documents
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.
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)
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.
The following code excerpt will overwrite the target directory if it already exists:
My.Computer.FileSystem.MoveDirectory("C:\Temp", "C:\Temp2", True)