Create a File Using VB.NET

This article explains how to create file in VB.NET.
  • 8506

Create a File

We can create a file in two different following methods

• File.Create
• File.CreateText

FileInfo.Create Method

The FileInfo.Create method creates a file at the given path. If just a file name is provided without a path, the file will be created in the current folder. The following code snippet creates a file using the Create method that returns a FileSteam object. The Write method of FileStream can be used to write text to the file.

Imports System.Text

Imports System.IO

Module Module1

    Dim InlineAssignHelper As Object

    Sub Main()

        ' Full file name

        Dim fileName As String = "C:\Temp\Text.txt"

        Dim fi As New FileInfo(fileName)

        Try

            ' Check if file already exists. If yes, delete it.

            If fi.Exists Then

                fi.Delete()

            End If

            ' Create a new file

            Using fs As FileStream = fi.Create()

                Dim txt As [Byte]() = New UTF8Encoding(True).GetBytes("New file.")

                fs.Write(txt, 0, txt.Length)

                Dim author As [Byte]() = New UTF8Encoding(True).GetBytes("Visual Basic")

                fs.Write(author, 0, author.Length)

            End Using

            ' Write file contents on console.

            Using sr As StreamReader = File.OpenText(fileName)

                Dim s As String = ""

                While (InlineAssignHelper(s, sr.ReadLine())) IsNot Nothing

                    Console.WriteLine(s)

                End While

            End Using

        Catch Ex As Exception

            Console.WriteLine(Ex.ToString())

        End Try

    End Sub

End Module

Output:

FileCreated.jpg

Open this text file:

File Created in VB.jpg

FileInfo.CreateText Method

The FileInfo.CreateText method creates and opens a file for writing UTF-8 encoded text. If file already exists, this method opens the file.
The following code snippet creates a file using the CreateText method that returns a StreamWriter object. The WriteLine method of SteamLine can be used to add line text to the object and writes to the file.

Imports System.Text

Imports System.IO

Module Module1

    Dim InlineAssignHelper As Object

    Sub Main()

        ' Full file name

        Dim fileName As String = "C:\Temp\Text.txt"

        Dim fi As New IO.FileInfo(fileName)

        Try

            ' Check if file already exists. If yes, delete it.

            If fi.Exists Then

                fi.Delete()

            End If

            ' Create a new file

            Using sw As StreamWriter = fi.CreateText()

 

                sw.WriteLine("New file created: {0}", DateTime.Now.ToString())

                sw.WriteLine("Author: Mahesh Chand")

                sw.WriteLine("Add one more line ")

                sw.WriteLine("Add one more line ")

                sw.WriteLine("Done! ")

            End Using

            ' Write file contents on console.

            Using sr As StreamReader = File.OpenText(fileName)

                Dim s As String = ""

                While (InlineAssignHelper(s, sr.ReadLine())) IsNot Nothing

                    Console.WriteLine(s)

                End While

            End Using

        Catch Ex As Exception

            Console.WriteLine(Ex.ToString())

        End Try

    End Sub

End Module

Output:

 Create-Text-Method-in-VB.jpg



Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.