简体   繁体   English

“Microsoft.VisualBasic.FileIO 不存在”。 如何在我的项目中包含这个库?

[英]“Microsoft.VisualBasic.FileIO does not exist”. How to include this library in my project?

So I am trying to parse a.csv file with columns that may include their own comma characters.所以我试图用可能包含自己的逗号字符的列来解析 a.csv 文件。

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. 此处此处的答案说,无需编写我自己的逐行解析器即可轻松处理此问题的最佳方法是使用 Microsoft.VisualBasic.FileIO 库。

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.这个问题的修复herehere都说我需要使用参考管理器将Microsoft.VisualBasic添加到我的项目中,但是我的参考管理器没有Assemblies,以及官方帮助文档说要使用的Framework和Extensions子组选项卡将 package 添加到我的项目中。 I have attached a screenshot of my Reference Manager window here.我在此处附上了我的参考管理器 window 的屏幕截图。 The Projects tab just shows a list of all the projects in my solution and the Shared Projects tab is blank. “项目”选项卡仅显示我的解决方案中所有项目的列表,“共享项目”选项卡为空白。 参考经理

I have also checked to see if I should include a NuGet package instead, but that did not solve the problem at all.我还检查了是否应该包含 NuGet package,但这根本没有解决问题。

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.我最终使用了CSVHelper 库,该库与 NuGet 一起安装得很好,并完美地处理了包含逗号的列,甚至可以配置为忽略空行和标题。

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.我不知道为什么 VisualBasic.FileIO 不能添加到我的项目中,为什么 6 多年前的最佳解决方案说要使用这个库,或者为什么我的 Reference Manager window 与官方帮助所说的不同,但我解决了我的根本问题,所以希望我将来实际上不需要使用参考管理器来做任何事情。

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.首先,请将您的平台从 .net core2.1 更改为 .net core3.0 或 .net core 3.1。

Second, you can refer to the following code to convert csv file to datatable by using其次,您可以参考以下代码将 csv 文件转换为数据表使用

Microsoft.VisualBasic.FileIO. 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:结果: 在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 microsoft.visualbasic.fileio 不存在 - microsoft.visualbasic.fileio does not exist “microsoft.visualbasic.fileio 不存在”尝试使用 TextFieldParser - “microsoft.visualbasic.fileio does not exist” trying to use TextFieldParser Microsoft.VisualBasic.FileIO 参考 - Microsoft.VisualBasic.FileIO reference .Net Core 无法检测到 Microsoft.VisualBasic.FileIO - .Net Core unable to detect Microsoft.VisualBasic.FileIO 命名空间“Microsoft.VisualBasic”中不存在类型或命名空间名称“FileIO” - The type or namespace name 'FileIO' does not exist in the namespace 'Microsoft.VisualBasic' 有没有办法使用 Microsoft.VisualBasic.FileIO 的 TextFieldParser 将 .csv 文件中的所有空字段映射为 NULL - Is there a way to map all the empty fields inside .csv file as NULL using Microsoft.VisualBasic.FileIO's TextFieldParser 有没有办法使用 Microsoft.VisualBasic.FileIO 修剪 .csv 文件中的所有字段 - Is there a way to trim all the fields inside .csv file using using Microsoft.VisualBasic.FileIO 解析FileIO在名称空间Microsoft.VisualBasic .NetCore Connsole App中不存在 - Resolve FileIO does not exist in nameespace Microsoft.VisualBasic .NetCore Connsole App 即使我添加了对项目的引用并包含 Microsoft.VisualBasic,Interaction.InputBox 也不存在 - Interaction.InputBox does not exist even though I add reference to project and include Microsoft.VisualBasic 如何编写与 Microsoft.VisualBasic.FileIO.TextFieldParser 兼容的 csv 格式? - How to write csv format compatible with Microsoft.VisualBasic.FileIO.TextFieldParser?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM