0
var reader = new StreamReader(File.OpenRead(@"C:\sample.csv"));
List<string> listA = new List<string>();
string line1;
Stream originalStream = null;
while (!reader.EndOfStream)
{                
    line1 = reader.ReadLine();
    listA.Add(line1);                
}
listA.RemoveAt(0);
originalStream = listA;

Here I am trying to remove first row and thats ok but after removing it I have other records in list of string which I want to write in a Stream variable. can anybody tell me how can i achieve that.

Thanks

3
  • That looks incredibly inefficient, RemoveAt is O(n) operation. Instead of removing the header, just skip from adding it to the list in the first place. And why would you want a list of string as stream? What are you actually trying to do here? Commented Nov 24, 2016 at 5:02
  • Do you want to write this list to a memory stream or to another file? Commented Nov 24, 2016 at 5:19
  • another stream not file Commented Nov 24, 2016 at 5:24

1 Answer 1

3

Reading all lines from file to a list and then removing one will not be fair, instead for whole those process i would like to suggest you a one liner like the following:

List<string> listA = System.IO.File.ReadAllLines(@"C:\sample.csv").Skip(1).ToList();

Use this lines to make Stream from List:

 var binFormatter = new BinaryFormatter();
 Stream streamList = null;
 binFormatter.Serialize(streamList, listA);
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, actually I am trying to build Biztalk pipeline component for that the output can only be in stream or streamreader as per my knowledge
is there a way to read the file in stream or streamreader and skip the first row(without list)
I had few updates to the answer, you can take a look
Hi I am getting below error "The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?)"
use using System.Runtime.Serialization.Formatters.Binary; to solve that error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.