Working with directories and files
We will utilize two different methods for VB.NET and older VB family members. We will see this pattern throughout this chapter as Microsoft added new file features to the .NET language that you can leverage in VB.NET and ASP.NET.
Working with files in VB.NET
In VB.NET, you use the File
class from the System.IO
namespace to work with files. The File
class provides static methods for performing various file-related operations.
To create a file, you can use the Create
method, which returns a FileStream
object you can later use to write data to the file:
Imports System.IO File.Create("C:\Path\File.txt")
To delete a file, you can use the Delete
method:
Imports System.IO File.Delete("C:\Path\ File.txt")
To check whether a file exists, you use the Exists
property, which returns a Boolean value indicating whether the file exists or not:
Imports System.IO If File.Exists("C:\Path\File.txt") Then ...