Read from CD

Elbob

Member
Joined
Apr 28, 2009
Messages
18
Programming Experience
1-3
Hi, I'm developing an application that will take the data from a cd. This cd stores a .csv file.

Can VB read this data from the disc and upload it to a database?

Im not sure exactly where to start looking. Can someone point me in the right direction, as to what compnents i should be looking at.

Any help appreciated

Thanks

:)
 
Reading a file on a CD drive shouldnt be any different than reading a file on a hard drive.
 
Yes, you can access file on cd's like a hard drive and flash drives.

Look into the System.IO namespace
 
I've managed to get it to browse the file system using

VB.NET:
        Dim getFile As New OpenFileDialog
        With getFile
            .Filter = "CSV files (*.csv)|*.csv"
            .FilterIndex = 1
        End With
        
        If getFile.ShowDialog() = Windows.Forms.DialogResult.OK Then
            FileShow.Text = getFile.FileName
        End If

Any ideas how can i upload the chosen file onto my hard drive? I'm intending to transfer the .csv data into a database (but ill worry about that later lol)

Thanks
 
You can use System.IO.File.Copy() to just copy the file and you can use a SaveFileDialog to find out where on the HDD to copy the file to, or you could use a hard coded path:
VB.NET:
        'Use two dialogs to copy the file
        Dim getFile As New OpenFileDialog

        With getFile
            .Filter = "CSV files (*.csv)|*.csv"
            .DefaultExt = "csv"
            .AddExtension = True
            .Multiselect = False
            .Title = "Locate CSV file"
        End With

        If getFile.ShowDialog(Me) <> Windows.Forms.DialogResult.Cancel Then
            FileShow.Text = getFile.FileName
            Dim putFile As New SaveFileDialog

            With putFile
                .FileName = System.IO.Path.GetFileName(getFile.FileName)
                .Filter = "CSV files (*.csv)|*.csv"
                .DefaultExt = "csv"
                .AddExtension = True
                .Title = "Save CSV file"
            End With

            If putFile.ShowDialog(Me) <> Windows.Forms.DialogResult.Cancel Then
                Try
                    System.IO.File.Copy(getFile.FileName, putFile.FileName)
                Catch
                    MessageBox.Show(ex.Message)
                End Try
            End If

        End If
 
Hi

New piece of code :)

VB.NET:
    Private Sub BrowseFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Browse.Click

        Dim getFile As New OpenFileDialog
        With getFile
            .Filter = "CSV files (*.csv)|*.csv"
            .FilterIndex = 1

        End With
        If getFile.ShowDialog() = Windows.Forms.DialogResult.OK Then
            FileShow.Text = getFile.FileName
        End If

    End Sub

    Private Sub UploadButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UploadButton.Click
        Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser _
        (getfile)
            MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
            MyReader.Delimiters = New String() {","}
            Dim currentRow As String()
            'Loop through all fields.
            While Not MyReader.EndOfData
                Try
                    currentRow = MyReader.ReadFields()
                    Console.Write(currentRow.ToString + " ")
                    Console.Write(currentRow(1).ToString + " ")
                    Console.Write(currentRow(2).ToString + " ")
                    Console.Write(currentRow(3).ToString + " ")
                Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                    MsgBox("Line " & ex.Message & _
                    " is invalid.  Skipping")
                End Try
            End While
        End Using
    End Sub

I want this code to read the .csv file line by line. How can i pass the chosen .csv file to the upload button?

Thanks

:)
 
This code is supposed to show each row in a cmd window. Its not working tho. Can anyone help?

VB.NET:
        Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser _
        ("path to file")
            MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
            MyReader.Delimiters = New String() {"'"}
            Dim currentRow As String()
            'Loop through all of the fields in the file. 
            'If any lines are corrupt, report an error and continue parsing. 
            While Not MyReader.EndOfData
                Try
                    currentRow = MyReader.ReadFields()

                    currentRow(0) = "Account No"
                    currentRow(1) = "Tel No"

                    Console.Write(currentRow.ToString(0) + " ")
                    Console.Write(currentRow(1).ToString + " ")

                    'Console.Write(currentRow(2).ToString + " ")
                    'Console.Write(currentRow(3).ToString + " ")

                Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                    MsgBox("Line " & ex.Message & _
                    " is invalid.  Skipping")
                End Try
            End While
        End Using
 
Last edited:
Back
Top