VB.Net - 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 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 −
Attributes
Gets the attributes for the current file or directory.
CreationTime
Gets the creation time of the current file or directory.
Exists
Gets a Boolean value indicating whether the directory exists.
Extension
Gets the string representing the file extension.
FullName
Gets the full path of the directory or file.
LastAccessTime
Gets the time the current file or directory was last accessed.
Name
Gets the name of this DirectoryInfo instance.
Following are some commonly used methods of the DirectoryInfo class −
Public Sub Create
Creates a directory.
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.
Public Overrides Sub Delete
Deletes this DirectoryInfo if it is empty.
Public Function GetDirectories As DirectoryInfo()
Returns the subdirectories of the current directory.
Public Function GetFiles As FileInfo()
Returns a file list from the current directory.
For complete list of properties and methods please visit Microsoft's documentation.
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 −
Attributes
Gets the attributes for the current file.
CreationTime
Gets the creation time of the current file.
Directory
Gets an instance of the directory, which the file belongs to.
Exists
Gets a Boolean value indicating whether the file exists.
Extension
Gets the string representing the file extension.
FullName
Gets the full path of the file.
LastAccessTime
Gets the time the current file was last accessed.
LastWriteTime
Gets the time of the last written activity of the file.
Length
Gets the size, in bytes, of the current file.
Name
Gets the name of the file.
Following are some commonly used methods of the FileInfo class −
Public Function AppendText As StreamWriter
Creates a StreamWriter that appends text to the file represented by this instance of the FileInfo.
Public Function Create As FileStream
Creates a file.
Public Overrides Sub Delete
Deletes a file permanently.
Public Sub MoveTo (destFileName As String )
Moves a specified file to a new location, providing the option to specify a new file name.
Public Function Open (mode As FileMode) As FileStream
Opens a file in the specified mode.
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.
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.
Public Function OpenRead As FileStream
Creates a read-only FileStream
Public Function OpenWrite As FileStream
Creates a write-only FileStream.
For complete list of properties and methods, please visit Microsoft's documentation
The following example demonstrates the use of the above-mentioned classes −
Imports System.IO
Module fileProg
   Sub Main()
      'creating a DirectoryInfo object
      Dim mydir As DirectoryInfo = New DirectoryInfo("c:\Windows")
      ' getting the files in the directory, their names and size
      Dim f As FileInfo() = mydir.GetFiles()
      Dim file As FileInfo
      
      For Each file In f
         Console.WriteLine("File Name: {0} Size: {1}  ", file.Name, file.Length)
      Next file
      Console.ReadKey()
   End Sub
End Module
When you compile and run the program, it displays the names of files and their size in the Windows directory.

    63 Lectures 
     4 hours 

 Frahaan Hussain

    103 Lectures 
     12 hours 

 Arnold Higuit

    60 Lectures 
     9.5 hours 

 Arnold Higuit

    97 Lectures 
     9 hours 

 Arnold Higuit
 Print
 Add Notes
 Bookmark this page