c# reading csv file gives not a valid path

前端 未结 7 1890
Happy的楠姐
Happy的楠姐 2020-12-05 07:04

I can\'t seem to read a .csv file using the following connection string:

var fileName = string.Format(\"{0}{1}\", AppDomain.CurrentDomain.BaseDirectory, \"Up         


        
相关标签:
7条回答
  • 2020-12-05 07:57

    If you're just trying to read a CSV file with C#, the easiest thing is to use the Microsoft.VisualBasic.FileIO.TextFieldParser class. It's actually built into the .NET Framework, instead of being a third-party extension.

    Yes, it is in Microsoft.VisualBasic.dll, but that doesn't mean you can't use it from C# (or any other CLR language).

    Here's an example of usage, taken from the MSDN documentation:

    Using MyReader As New _
    Microsoft.VisualBasic.FileIO.TextFieldParser("C:\testfile.txt")
       MyReader.TextFieldType = FileIO.FieldType.Delimited
       MyReader.SetDelimiters(",")
       Dim currentRow As String()
       While Not MyReader.EndOfData
          Try
             currentRow = MyReader.ReadFields()
             Dim currentField As String
             For Each currentField In currentRow
                MsgBox(currentField)
             Next
          Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
          MsgBox("Line " & ex.Message & _
          "is not valid and will be skipped.")
          End Try
       End While
    End Using
    

    Again, this example is in VB.NET, but it would be trivial to translate it to C#.

    0 讨论(0)
提交回复
热议问题