In this section, you’ll learn how to access and manipulate files and folders with the help of the Directory and File classes of the System.IO namespace. The Directory class provides methods for manipulating folders, and the File class provides methods for manipulating files. These two objects allow you to perform just about any of the usual operations on folders and files, respectively, short of storing data into or reading from files. By the way, directory is another name for folder; the two terms mean the same thing, but folder is the more-familiar term in Windows. When it comes to developers and administrators, Microsoft still uses directory (the Active Directory, the Directory object, and so on), especially with command-line utilities.
Keep in mind that Directory and File objects don’t represent folders or files. Directory and File are shared classes, and you must supply the name of the folder or file they will act upon as an argument to the appropriate method. The two classes that represent folders and files are the DirectoryInfo and FileInfo classes. If you’re in doubt about which class you should use in your code, consider that the members of the Directory and File classes are shared: You can call them without having to explicitly create an instance of the corresponding object first, and you must supply the name of the folder or file their methods will act upon as an argument. The methods of the DirectoryInfo and FileInfo classes are instance methods: Their methods apply to the folder or file represented by the current instance of the class.
Both the Directory and the DirectoryInfo classes allow you to delete a folder, including its subfolders. The Delete method of the DirectoryInfo class will act on a directory you specified when you instantiated the class:
Dim DI As New System.IO.DirectoryInfo("C:\Work Files\Assignments")
DI.Delete()
Code language: CSS (css)
But you can’t call Delete on a DirectoryInfo object that you haven’t specifically declared. The DirectoryInfo.Delete method doesn’t accept the name of a folder as an argument. The Delete method of the Directory class, on the other hand, deletes the folder passed as an argument to the method:
System.IO.Directory.Delete("C:\Work Files\Assignments")
Code language: CSS (css)