Convert CSV file to TXT file in .NET using vb (windows form)

Convert .csv(comma separated value) file to .txt file in vb.net
Our written program will ignore commas in any particular field. I have made a windows application in vb.net which will take a .csv file as input using open file dialogue and give a .txt file as output in a folder that is chosen by the user using folder browser dialogue.
The code is given below. You can add .csv extension checking in open file dialogue.
Imports Microsoft.VisualBasic.FileIO
Public Class Form1
Private Sub btn_start_Click(sender As Object, e As EventArgs) Handles btn_start.Click
Dim filename As String = System.IO.Path.GetFileNameWithoutExtension(txt_source.Text)
Dim tfp As New TextFieldParser(txt_source.Text)
tfp.Delimiters = New String() {","}
tfp.TextFieldType = FieldType.Delimited
Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter(txt_destination.Text + "\" + filename + ".txt", True)
file.WriteLine("Name" & ControlChars.Tab & "Address" & ControlChars.Tab & "Phone No.")
tfp.ReadLine() ' skip header
While tfp.EndOfData = False
Dim fields = tfp.ReadFields()
file.WriteLine(fields(0) & ControlChars.Tab & fields(1) & ControlChars.Tab & fields(2))
End While
file.Close()
MessageBox.Show("Conversion Successful", "Converter", MessageBoxButtons.OK, MessageBoxIcon.None)
End Sub
Private Sub btn_destination_Click(sender As Object, e As EventArgs) Handles btn_destination.Click
If (FolderBrowserDialog1.ShowDialog() = DialogResult.OK) Then
txt_destination.Text = FolderBrowserDialog1.SelectedPath
End If
End Sub
Private Sub btn_source_Click(sender As Object, e As EventArgs) Handles btn_source.Click
OpenFileDialog1.Title = "Please Select a File"
OpenFileDialog1.InitialDirectory = "C:\"
OpenFileDialog1.ShowDialog()
OpenFileDialog1.RestoreDirectory = True
End Sub
Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim strm As System.IO.Stream
strm = OpenFileDialog1.OpenFile()
txt_source.Text = OpenFileDialog1.FileName.ToString()
End Sub
End Class

Windows Form to select input and output location

Conversion Successful

Sample CSV

Sample TXT
Download Source Code:
Below is the given link to download the source code:
Leave a Reply