VB.Net - Manipulating the Windows File System

VB.Net allows you to work with the directories and files using various directory and file-related classes like, the DirectoryInfo class and the FileInfo class.

The DirectoryInfo Class

The DirectoryInfo class is derived from the FileSystemInfo class. It has various methods for creating, moving, and browsing through directories and subdirectories. This class cannot be inherited.

Following are some commonly used properties of the DirectoryInfo class:

S.N

Property Name & Description

1

AttributesGets the attributes for the current file or directory.

2

CreationTimeGets the creation time of the current file or directory.

3

ExistsGets a Boolean value indicating whether the directory exists.

4

ExtensionGets the string representing the file extension.

5

FullNameGets the full path of the directory or file.

6

LastAccessTimeGets the time the current file or directory was last accessed.

7

NameGets the name of this DirectoryInfo instance.

Following are some commonly used methods of the DirectoryInfo class:

S.N

Method Name & Purpose

1

Public Sub CreateCreates a directory.

2

Public Function CreateSubdirectory (
path As String
) As DirectoryInfo
Creates a subdirectory or subdirectories on the specified path. The specified path can be relative to this instance of the DirectoryInfo class.

3

Public Overrides Sub DeleteDeletes this DirectoryInfo if it is empty.

4

Public Function GetDirectories As DirectoryInfo()Returns the subdirectories of the current directory.

5

Public Function GetFiles As FileInfo()Returns a file list from the current directory.

The FileInfo Class

The FileInfo class is derived from the FileSystemInfo class. It has properties and instance methods for creating, copying, deleting, moving, and opening of files, and helps in the creation of FileStream objects. This class cannot be inherited.

Following are some commonly used properties of the FileInfo class:

S.N

Property Name & Description

1

AttributesGets the attributes for the current file.

2

CreationTimeGets the creation time of the current file.

3

DirectoryGets an instance of the directory, which the file belongs to.

4

ExistsGets a Boolean value indicating whether the file exists.

5

ExtensionGets the string representing the file extension.

6

FullNameGets the full path of the file.

7

LastAccessTimeGets the time the current file was last accessed.

8

LastWriteTimeGets the time of the last written activity of the file.

9

LengthGets the size, in bytes, of the current file.

10

NameGets the name of the file.

Following are some commonly used methods of the FileInfo class:

S.N

Method Name & Purpose

1

Public Function AppendText As StreamWriterCreates a StreamWriter that appends text to the file represented by this instance of the FileInfo.

2

Public Function Create As FileStreamCreates a file.

3

Public Overrides Sub DeleteDeletes a file permanently.

4

Public Sub MoveTo (
destFileName As String
)
Moves a specified file to a new location, providing the option to specify a new file name.

5

Public Function Open (
mode As FileMode
) As FileStream
Opens a file in the specified mode.

6

Public Function Open (
mode As FileMode,
access As FileAccess
) As FileStream
Opens a file in the specified mode with read, write, or read/write
access.

7

Public Function Open (
mode As FileMode,
access As FileAccess,
share As FileShare
) As FileStream
Opens a file in the specified mode with read, write, or read/write access and the specified sharing option.

8

Public Function OpenRead As FileStreamCreates a read-only FileStream

9

Public Function OpenWrite As FileStreamCreates a write-only FileStream.