Search code examples
c#file-handlingcsvhelper

How to remove Header from a CSV FileStream


I have to work on a certain set of limitations for csv file upload :

  • I will be working with 'large' CSV files (containing header row)
  • I need to remove the first header row from the CSV file
  • The file-upload code needs a FileStream (not containing the header) as input! (as I am restricted to do a lot of stream operations on top of this stream (containing headerless csv data))

Wrapper C# Code :

using (var stream = File.OpenRead("C:\~~~\~~~\~~~\SampleFile.csv"))
{
    //CSV Header removal snippet - which gives me a new stream containing data without headers.
    ~
    ~
    ~
    ~
    //All my stream handling code of chunking stream into 100mb and then uploading each chunk to azure storage (which is not part of this question)
}

Now I already know - that I can simply remove headers of a csv file using libraries like - CSVHelper
(How to exclude header when writing data to CSV)

Using the above way I can create a header-less copy of a file and read the new file back as FileStream - but the problem is that I'm dealing with large files and making a copy of a file just to remove headers will be a space-consuming job.

So for the first time - I am asking a question in StackOverflow - to find a good solution to the above problem. I hope I was able to explain the problem clearly.


Solution

  • This should work to seek to the end of the first line.

    using (var stream = File.OpenRead("~~filepath~~"))
    using (var reader = new StreamReader(stream))                
    {
        string line = null;
        if ((line = reader.ReadLine()) != null)
        {        
            stream.Position = line.Length + 2;
            // The 2 is for NewLine(\r\n)
        }
    
        //All my stream handling code of chunking stream into 100mb and then uploading each chunk to azure storage (which is not part of this question)   
    }