[Solved] NON QUOTATION MARK IN CSV FILE BUT IN TXT FILE IT HAVE


Firstly, when posting code snippets, please format them for readability. I have fixed that for you.

Secondly, there’s no such thing as “converting” between a CSV file and a text file. A CSV is just plain text. If the text contains record and field delimiters then it is a CSV file, otherwise it’s not. It’s plain text either way.

As for the question, I would suggest writing a CSV with quoted values something like this:

Dim line = String.Format("""{0}"",""{1}"",""{2}""",
                         firstValue,
                         secondValue,
                         thirdValue)

The use of String.Format or, in VB 2015, string interpolation makes the code far more readable. As for putting a double-quote in a literal string, you simply escape it with another double-quote. You might also use the ControlChars.Quote field:

Dim line = String.Format("{0}{1}{0},{0}{2}{0},{0}{3}{0}",
                         ControlChars.Quote,
                         firstValue,
                         secondValue,
                         thirdValue)

Note that it is generally only text values that get quoted, not numbers and dates and the like. That’s because the main reason for the quoting is to allow a value to contain delimiters, i.e. commas or line breaks. If none of your values do contain delimiters then you can safely omit the quotes altogether.

EDIT: As an example, here’s how I might write a DataTable to a CSV file where the first and fourth columns contain String values, the second column contains numbers and the third column contains Date values.

Private Sub WriteToCsv(table As DataTable, path As String)
    Dim lines As New List(Of String)

    For Each row As DataRow In table.Rows
        lines.Add(String.Format("""{0}"",{1},{2:yyyy-MM-dd},""{3}""",
                                row(0),
                                row(1),
                                row(2),
                                row(3)))
    Next

    IO.File.WriteAllLines(path, lines)
End Sub

3

solved NON QUOTATION MARK IN CSV FILE BUT IN TXT FILE IT HAVE