Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检查CSV文件是否为空/避免引发异常_C#_Winforms_Csv_Csvhelper - Fatal编程技术网

C# 检查CSV文件是否为空/避免引发异常

C# 检查CSV文件是否为空/避免引发异常,c#,winforms,csv,csvhelper,C#,Winforms,Csv,Csvhelper,如果我对一个完全为空的csv文件调用csv.Read(),我会得到一个异常。有没有一种方法可以检查CSV而不必退回到捕捉块上 var csv = new CsvReader(csvFile); try { while (csv.Read()) { // process the CSV file... } } catch (CsvReaderException) { // Handles this error (when attempting to

如果我对一个完全为空的csv文件调用
csv.Read()
,我会得到一个异常。有没有一种方法可以检查CSV而不必退回到捕捉块上

var csv = new CsvReader(csvFile);

try
{
    while (csv.Read())
    {
        // process the CSV file...
    }
}
catch (CsvReaderException)
{
    // Handles this error (when attempting to call "csv.Read()" on a completely empty CSV):
    // An unhandled exception of type 'CsvHelper.CsvReaderException' occurred in CsvHelper.dll
    // Additional information: No header record was found.
    MessageBox.Show(MessageBoxErrorMessageExpectedColumns, MessageBoxErrorMessageCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
    return null;
}

您可以简单地检查文件的长度是否为零

var csvFileLenth = new System.IO.FileInfo(path).Length;

if( csvFileLenth != 0)
{
  try
  {
    while (csv.Read())
    {
      // process the CSV file...
    }
  }
  catch (CsvReaderException)
  {
    // Handles this error (when attempting to call "csv.Read()" on a completely empty CSV):
    // An unhandled exception of type 'CsvHelper.CsvReaderException' occurred in CsvHelper.dll
    // Additional information: No header record was found.
    MessageBox.Show(MessageBoxErrorMessageExpectedColumns,              MessageBoxErrorMessageCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
    return null;
  }

}

您可以简单地检查文件的长度是否为零

var csvFileLenth = new System.IO.FileInfo(path).Length;

if( csvFileLenth != 0)
{
  try
  {
    while (csv.Read())
    {
      // process the CSV file...
    }
  }
  catch (CsvReaderException)
  {
    // Handles this error (when attempting to call "csv.Read()" on a completely empty CSV):
    // An unhandled exception of type 'CsvHelper.CsvReaderException' occurred in CsvHelper.dll
    // Additional information: No header record was found.
    MessageBox.Show(MessageBoxErrorMessageExpectedColumns,              MessageBoxErrorMessageCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
    return null;
  }

}

您可以在源csvfile路径上使用
File.Exists

if (File.Exists("csvfile"))
{
   //process
}

编辑:抱歉,误读了您的帖子,您确实需要将此内容与上面的答案结合起来,以检查是否存在空文件。

您可以在源csvfile路径上使用
file.Exists

if (File.Exists("csvfile"))
{
   //process
}
编辑:抱歉,误读您的帖子,您确实需要将此内容与上面的答案结合起来,以检查是否存在空文件。

使用文件信息

if (new FileInfo("yourfilename").Length == 0)
{
//Do something here
}
else
{
//Do something else here
}
您可能还应该检查以确保该文件存在,因为如果该文件不存在,它将抛出FileNotFoundException。

使用FileInfo

if (new FileInfo("yourfilename").Length == 0)
{
//Do something here
}
else
{
//Do something else here
}

您可能还应该检查以确保该文件存在,因为如果该文件不存在,它将抛出FileNotFoundException。

我认为这是使用try/catch的好时机……我认为这是使用try/catch的好时机。。。