Read from Memory Stream Problem

Jul 24, 2012 at 3:10 PM

Hi, I'm starting to use this library I'm facing with a problem that I can't resolve...

The thing is that I'm using a stream retrieved from a ftp folder, and before use in the CsvReader, I'm parsing that stream to a MemoryStream...

The thing is that with that parsing, the CsvReader it doesn't retrieve any records...

I don't know why, because the MemoryStream is fine... everything seems to be ok,... in fact, it doesn't throw me any exception... but I know that this csv has records, and some how, it doesn't retrieve them.

Here is the code that I'm using just as an example:

        static void Main(string[] args)
        {
            using (var streamReader = new StreamReader([CsvPath])
            {
                var memoryStream = ReadFully(streamReader.BaseStream);
                using (CsvReader csv = new CsvReader(memoryStream))
                {
                    /*
                     * here csv.DataRecords it telling me 'Enumeration yielded no results'
                     */
                }
            }
        }

        static MemoryStream ReadFully(Stream input)
        {
            byte[] buffer = new byte[16 * 1024];
            MemoryStream ms = new MemoryStream();
            int read;
            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
            return ms;
        }

The thing is that, if I remove the parsing to a memoryStream, and just use the BaseStream of the StreamReader it works fine...

BUT I NEED TO PARSE TO A MEMORY STREAM, BECAUSE I'M RETRIVING A STREAM FROM A FTP, AND I'M USING IT IN ANOTHER PLACES...

Thanks in advance!!

Coordinator
Jul 25, 2012 at 8:40 AM

After reading to your memory stream, you aren't resetting its position. Hence, the memory stream is at its end. Set its Position property to zero after reading it.

Jul 25, 2012 at 3:26 PM
Edited Jul 25, 2012 at 3:26 PM

That works really great... I didn't know that need to reseed the position...

Really thanks on that...