0

So I am trying to parse a .csv file with columns that may include their own comma characters.

Answers here and here say that the best way to easily handle this without writing my own line-by-line parser would be to use the Microsoft.VisualBasic.FileIO library.

Unfortunately, when i try to include this in my project I get the common error:

'File.IO' does not exist in the namespace "Microsoft.VisualBasic" (are you missing an assembly reference?)

Fixes for this issue here and here all say that I need to use the Reference Manager to add Microsoft.VisualBasic to my project, but my reference manager does not have the Assemblies, with Framework and Extensions subgroups tabs that the official help documents say to use to add the package to my project. I have attached a screenshot of my Reference Manager window here. The Projects tab just shows a list of all the projects in my solution and the Shared Projects tab is blank. Reference Manager

I have also checked to see if I should include a NuGet package instead, but that did not solve the problem at all.

Brandon Miller
  • 327
  • 1
  • 4
  • 11
  • 2
    you want use Microsoft.VisualBasic.IO in C# project instead of System.IO.File? – daremachine May 28 '20 at 01:04
  • 1
    I can add Microsoft.VisualBasic NuGet package in "Manage Nuget packages..." in VS2019 – daremachine May 28 '20 at 01:11
  • Are you targeting .NET Core 2.x? – Martheen May 28 '20 at 01:13
  • Please create .NET Framework based projects. .NET Core's VB support is incomplete. – Lex Li May 28 '20 at 04:18
  • yes .NET Core 2.1 And I just want to use whatever will work, it doesn't matter to me if I use System.IO.File or not but I haven't seen any suggestions to use that library or which calls in that library to use so I tried to use the other library. Does System.IO.File have a way to read csv lines and handle embedded commas in columns? I added the Microsoft.VisualBasic package through NuGet but it didn't give me the .FileIO functions so I assume the "Add Reference" does something different from NuGet. – Brandon Miller May 28 '20 at 16:48

2 Answers2

0

I ended up using the CSVHelper library which installs fine with NuGet and handled the comma-contained columns perfectly and even could be configured to ignore blank lines and headers.

I do not know why VisualBasic.FileIO could not be added to my project, why the best solutions from 6+ years ago said to use this library, or why my Reference Manager window is different from what official help says it should be, but I solved my root problem so here's hoping I don't actually need to use the Reference Manager in the future for anything.

Brandon Miller
  • 327
  • 1
  • 4
  • 11
0

I find the solution for solving the problem.

First, please change your platform from .net core2.1 to .net core3.0 or.net core 3.1.

Second, you can refer to the following code to convert csv file to datatable by using

Microsoft.VisualBasic.FileIO.

static void Main(string[] args)
        {
            DataTable table = GetDataTabletFromCSVFile("D:\\Test.csv");
        }

        private static DataTable GetDataTabletFromCSVFile(string csv_file_path)
        {
            DataTable csvData = new DataTable();
            try
            {
                using (TextFieldParser csvReader = new TextFieldParser(csv_file_path))
                {
                    csvReader.SetDelimiters(new string[] { "," });
                    csvReader.HasFieldsEnclosedInQuotes = true;
                    string[] colFields = csvReader.ReadFields();
                    foreach (string column in colFields)
                    {
                        if (column == "Name" || column == "Age" || column == "Id")
                        {
                            DataColumn datecolumn = new DataColumn(column);
                            datecolumn.AllowDBNull = true;
                            csvData.Columns.Add(datecolumn);
                        }
                    }
                    while (!csvReader.EndOfData)
                    {
                        string[] fieldData = csvReader.ReadFields();
                        for (int i = 0; i < fieldData.Length; i++)
                        {
                            if (fieldData[i] == "")
                            {
                                fieldData[i] = null;
                            }
                        }
                        csvData.Rows.Add(fieldData);
                    }
                }

            }
            catch (Exception)
            {
                throw;
            }
            return csvData;
        }

Result: enter image description here

Jack J Jun
  • 5,633
  • 1
  • 9
  • 27