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

时间:2016-06-23 18:29:45

标签: c# csv embedded-resource

我在同一项目中有一个csv文件,其Build Action为“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() + ";  ";
                        }
                    }
                }
            }

1 个答案:

答案 0 :(得分:2)

尝试 resourceName

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 - &gt; NS.MyList.csv

获取名字

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