Introduction to File handling methods in VB.NET

Tutorials PanelPosted by

In this article I will shed some light on the most used methods in VB.NET for file handling. File handling is important when it comes to storing and retrieving data using the file system. These functions and methods are in the IO namespace:

System.IO

Different and most used methods for handling file in VB.NET are mentioned below.

FILE HANDLING

1. AppendAllText(String,String)

This method will open the file, which is the first argument, and then appends a specific text to that file, which is the second argument, then closes the file. If the file does not exist, this method will create the file, then appends and close the file.

Example:

    Private Sub AppendToFile(ByVal FilePath As String, ByVal AppendTest As String)
        IO.File.AppendAllText(FilePath, AppendTest)
    End Sub

Call The function:

   AppendToFile("c:\MyTest.txt", "This is an AppendAllText Example")

2. Copy(String, String)

This function copies an existing file to a new file with a different name. The first argument is the path of the file to be copied, the second argument is for the destination path and file name to be created.
Note: this function will not overwrite a file if the destination name and path are the same as the original.

Example:

      IO.File.Copy("c:\MyTest.txt", "c:\MyTest_backup.txt")

3. Copy(String, String, Boolean)

This function copies an existing file to a new file. The first argument is for the path of the file to be copied. The second argument is to the destination and file name of the file to be created. The last argument is to allow an existing file to be overwritten if the value is set to true.

Example:

     File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName), True)

4. File.Create Method (String)

This method will create a file to a specified path. I will overdrive a file if it exists.

Example:

        Dim path As String = "c:\MyTest.txt"
        Dim fs As IO.FileStream = IO.File.Create(path)

5. Delete(String)

This function deletes mentioned file/files available from disk. Files are just deleted and never moved to the recycle-bin.

Example:

    Private Sub DeleteFile(ByVal FileName As String)
        Try
            IO.File.Delete(FileName)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

6. Exists(String)

This function returns a Boolean value which determines if a specific file exists or not.

Example:

        Dim filePath As String = "c:\test.txt"
        If IO.File.Exists(filePath) Then
            MessageBox.Show("File exists")
        Else
            MessageBox.Show("File does not exist")
        End If

7. Move(String)

This method moves a specific file (first argument) to a new location (second argument). The file can have a different name that its original.

Example:

        Dim path1 As String = "c:\MyTest.txt"
        Dim path2 As String = "c:\backup\MyTest_backup.txt"
        IO.File.Move(path1, path2)

8. Open(String)

This method opens a FileStream with read/write access.

Example:

        Dim path As String = "c:\MyTest.txt"
        Dim fs As IO.FileStream = IO.File.Open(path, IO.FileMode.Open)
        Dim enc As System.Text.UTF8Encoding = New System.Text.UTF8Encoding(True)
        Dim b(1024) As Byte
        Do While fs.Read(b, 0, b.Length) > 0
            MessageBox.Show(enc.GetString(b))
        Loop

9. OpenRead(String)

This method will open a file for reading.

Example:

        Dim path As String = "c:\MyTest.txt"
        Dim fs As IO.FileStream = IO.File.OpenRead(path)
        Dim b(1024) As Byte
        Dim enc As System.Text.UTF8Encoding = New System.Text.UTF8Encoding(True)

        Do While fs.Read(b, 0, b.Length) > 0
            Console.WriteLine(enc.GetString(b))
        Loop

10. WriteAllLines(String, String())

This method creates a new file (first argument), write a string array to that file (second argument), then closes the file.

Example:

        Dim path As String = "c:\MyTest.txt"
        If IO.File.Exists(path) = False Then
            Dim ArrayData() As String = {"Welcome", "to", "TutorialsPanel.com"}
            IO.File.WriteAllLines(path, ArrayData)
        End If

 

Related Articles:

Leave a Reply

Your email address will not be published. Required fields are marked *