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 Console.Writeline("The file exists") End If
You use the Copy
method to copy a file from one location to another:
Imports System.IO File.Copy("C:\Path\File.txt", "C:\Path\NewFile.txt")
To move or rename a file, you use the Move
method:
Imports System.IO File.Move("C:\Path\File.txt", "C:\Path\ NewFile.txt")
To retrieve the file attributes, use the GetAttributes
method and the FileAttributes
class:
Imports System.IO Dim attr As FileAttributes = File.GetAttributes("C:\Path\ File.txt") If attr.HasFlag(FileAttributes.ReadOnly)Then Console.Writeline("The file is read-only") End If If attr.HasFlag(FileAttributes.Hidden) Then Console.Writeline("The file is hidden") End If
Next, we will look at working with directories in VB.NET.
Working with directories in VB.NET
In VB.NET, you can use the Directory
class from the System.IO
namespace to work with directories. The Directory
class provides static methods for performing various directory-related operations.
To create a directory, you use the CreateDirectory
method:
Imports System.IO Directory.CreateDirectory("C:\Path\NewDirectory")
To delete a directory, you use the Delete
method. With the second parameter, you can specify whether to delete the directory recursively or not:
Imports System.IO Directory.Delete("C:\Path\Directory", recursive:=True)
To check whether a directory exists, you use the Exists
property, which returns a Boolean value indicating whether the directory exists or not:
Imports System.IO If Directory.Exists("C:\Path\Directory") Then Console.Writeline("The directory exists") End If
To move or rename a directory, you can use the Move
method:
Imports System.IO Directory.Move("C:\Path\OldDir", "C:\Path\NewDir")
To retrieve the files or subdirectories within a directory, you use the GetFiles
and Get
Directories
methods:
Imports System.IO Dim files As String() = Directory.GetFiles("C:\Path\Dir") Dim dirs As String() = Directory.GetDirectories("C:\Path\Dir")
The GetFiles
and GetDirectories
methods return collections of values, which we will explore in Chapter 10.
To retrieve information about a directory, you can use the GetDirectoryInfo
method to get a DirectoryInfo
object. You can use this DirectoryInfo
object to get files and directories, as in our last example, but from the object:
Imports System.IO Dim dirInfo As DirectoryInfo = My.Computer.FileSystemGetDirectoryInfo("C:\Path\Dir") Dim files() As FileInfo = dirInfo.GetFiles()
You retrieve various attributes of a directory using the GetAttributes
method, just as you did for a file previously:
Imports System.IO Dim attr As FileAttributes = File.GetAttributes("C:\Path\Dir") If attr.HasFlag(FileAttributes.ReadOnly) Then Console.Writeline("The directory is read-only") End If If attr.HasFlag(FileAttributes.Hidden) Then Console.Writeline("The directory is hidden") End if
Next, we will look at working with files in VBScript, VBA, and VB6.
Working with files in older VB family members
Working with files and directories in older VB family members (VBScript, VBA, and VB6) involves using the file system object (FSO) provided by the Windows Script Host. The FSO allows you to perform many operations on files and directories.
To create a directory with FSO, you use the CreateFolder
method:
Set myFSO = CreateObject("Scripting.FileSystemObject") myFSO.CreateFolder("C:\Path\Directory")
To delete a directory with FSO, you use the DeleteFolder
method. The second parameter specifies whether to delete the directory recursively or not:
Set myFSO = CreateObject("Scripting.FileSystemObject") myFSO.DeleteFolder("C:\Path\Directory", True)
To check whether a directory exists with FSO, you can use the FolderExists
method, which returns a Boolean value indicating whether the directory exists or not:
Set myFSO = CreateObject("Scripting.FileSystemObject") If myFSO.FolderExists("C:\Path\Directory") Then Wscript.Echo "The directory exists" End If
To rename or move a directory with FSO, you use the MoveFolder
method:
Set myFSO = CreateObject("Scripting.FileSystemObject") myFSO.MoveFolder "C:\Path\OldDir", C:\Path\NewDir"
Next, we will look at working with directories in VBScript, VBA, and VB6.
Working with directories in older VB family members
To create a file with FSO, you use the CreateTextFile
method, which has optional second and third parameters to specify whether the file should be overwritten and whether it should be created in Unicode format:
Set myFSO = CreateObject("Scripting.FileSystemObject") Set myFile = myFSO.CreateTextFile("C:\Path\File.txt", True)
To delete a file with FSO, you use the DeleteFile
method, which has an optional second parameter to specify whether only read-only files should be deleted:
Set myFSO = CreateObject("Scripting.FileSystemObject") myFSO.DeleteFile("C:\Path\File.txt")
To check whether a file exists with FSO, you use the FileExists
method, which returns a Boolean value indicating whether the file exists or not:
Set myFSO = CreateObject("Scripting.FileSystemObject") If myFSO.FileExists("C:\Path\File.txt") Then Wscript.Echo "The file exists" End If
To rename or move a file with FSO, you use the MoveFile
method:
Set myFSO = CreateObject("Scripting.FileSystemObject") myFSO.MoveFile "C:\Path\Old.txt", "C:\Path\New.txt"
Next, we will look at writing data in text format into files.