从 csv 文件创建 Stream 时出现异常

Exception when creating a Stream from csv file

我在同一项目中有一个 csv 文件,其构建操作为 "Embedded Resource"。当我尝试使用此文件创建流时,出现以下错误:

system.io.stream.null.readtimeout threw an exception of type system.invalidoperationexception

当我让它 运行 时,我发现了一个异常:

Value cannot be null.\r\nParameter name: stream

我不确定是什么原因造成的?是否有可能由于某种原因它没有获取我的文件,因此没有任何内容传递到流中?

internal static void InsertPropertyDefinitions()
        {
            DataTable csvData = null;

            string[] columnNames = null;
            int rowCount = 0;
            string[] streamDataValues = null;
            try {
                using (Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(
                    "My csv file path"))
                {
                    StreamReader streamReader = new StreamReader(stream);
                    while (!streamReader.EndOfStream)
                    {
                        String streamRowData = streamReader.ReadLine().Trim();
                        if (streamRowData.Length > 0)
                        {
                            streamDataValues = streamRowData.Split(',');
                            if (rowCount == 0)
                            {
                                rowCount = 1;
                                columnNames = streamRowData.Split(',');
                                csvData = new DataTable();

                                foreach (string csvColumn in columnNames)
                                {
                                    DataColumn dataColumn = new DataColumn(csvColumn.ToUpper(), typeof(string));

                                    dataColumn.DefaultValue = string.Empty;

                                    csvData.Columns.Add(dataColumn);
                                }
                            }
                            else
                            {
                                DataRow dataRow = csvData.NewRow();

                                for (int i = 0; i < columnNames.Length; i++)
                                {
                                    dataRow[columnNames[i]] = streamDataValues[i] == null ? string.Empty : streamDataValues[i].ToString();
                                }

                                csvData.Rows.Add(dataRow);
                            }
                        }
                    }
                    streamReader.Close();
                    streamReader.Dispose();

                    foreach (DataRow dataRow in csvData.Rows)
                    {
                        string rowValues = string.Empty;

                        foreach (string csvColumn in columnNames)
                        {
                            rowValues += csvColumn + "=" + dataRow[csvColumn].ToString() + ";  ";
                        }
                    }
                }
            }

尝试 资源名称 :

var assembly = Assembly.GetExecutingAssembly();
var resourceName = "MyCompany.MyProduct.MyFile.csv";

using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
    ...
}

资源名不一定等于文件名。您可以使用 Assembly.GetManifestResourceNames();

检查

通常 VS 将命名空间添加到资源名称。然后 MyList.csv -> NS.MyList.csv

获取名字

 var assembly = System.Reflection.Assembly.GetExecutingAssembly();
 var lst = assembly.GetManifestResourceNames();
 Console.WriteLine("Files");
 Console.WriteLine(string.Join(", ", lst));