C#, byte streaming with StreamReader problem

jazzIIIlove
jazzIIIlove used Ask the Experts™
on
Hi there;

I am trying to do byte streaming with C#. The application must reach the FTP server (which he can) and grab the file (which is not a text file), some encrypted file.

I can reach the server and list the directories with no problem. My problem is regarding streaming the file content. In fact, I have a compile time error in the lines:
int bytesRead = reader.Read(buffer, 0, buffer.Length);
and in the similar line in while.

I am sure it is true since the function takes the exact params but the compiler says:

Error 1 The best overloaded method match for 'System.IO.TextReader.Read(char[], int, int)' has some invalid arguments      
Error 2 Argument '1': cannot convert from 'byte[]' to 'char[]'      

My code is as follows:

Stream responseStream = response.GetResponseStream();
...
StreamReader reader = new StreamReader(responseStream);
MemoryStream memStream = new MemoryStream();
int Size = 1024;
            byte[] buffer = new byte[Size];
            int bytesRead = reader.Read(buffer, 0, buffer.Length);
            while (bytesRead != 0) //while(true)
                {
                    //Try to read the data
                    bytesRead = reader.Read(buffer, 0, buffer.Length);
                      //Write the downloaded data
                        memStream.Write(buffer, 0, bytesRead);
                       }
                byte[] downloadedData = new byte[0];
                //Convert the downloaded stream to a byte array
                downloadedData = memStream.ToArray();
...

Best regards.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
why not do this:?
Stream responseStream = response.GetResponseStream();
...
MemoryStream memStream = new MemoryStream();
int Size = 1024;
            byte[] buffer = new byte[Size];
            int bytesRead = responseStream.Read(buffer, 0, buffer.Length);
            while (bytesRead != 0) //while(true)
                {
                    //Try to read the data
                    bytesRead = reader.Read(buffer, 0, buffer.Length);
                      //Write the downloaded data
                        memStream.Write(buffer, 0, bytesRead);
                       }
                byte[] downloadedData = new byte[0];
                //Convert the downloaded stream to a byte array
                downloadedData = memStream.ToArray();

Open in new window

I couldn't see the difference with my original code in the question. What did you change?
Also, when I put your segment to my code, nothing changes, same error in the same line.
Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

Commented:
I am afraid you must have misread that error then as if an error occurred it would be a different one.

What I changed was two lines from your code:
I removed this line:
StreamReader reader = new StreamReader(responseStream);

And changed this line from:
int bytesRead = reader.Read(buffer, 0, buffer.Length);
to:
int bytesRead = responseStream.Read(buffer, 0, buffer.Length);
Miguel OzSenior Software Engineer
Top Expert 2009
Commented:
The compile error happens because Streamreader only handles text based stream (text files)
For binary files you need to use BinaryReader and BinaryWriter.
http://dotnetperls.com/binaryreader
http://msdn.microsoft.com/en-us/library/system.io.binaryreader.aspx
The links will give the basics on how to use them to achieve your results.
You have to start by replacing
StreamReader reader = new StreamReader(responseStream);
with
BinaryReader reader = new BinaryReader(responseStream);
and so on.

>>Omego2K:

Sorry, I am a little tired. I missed it.

My issue is ok, i can read txt now, but the file i am going to read is an encrypted file that I want to store it to my localdrive. I can read txt files, but simply, cannot read the encrpypted file. No bytes are coming from that file. What did I miss?
mas_oz2003:
Will the idea you are giving work on my issue?

Best regards.
Miguel OzSenior Software Engineer
Top Expert 2009

Commented:
Yes, it will work because the encrypted file can be treated as binary file. you will recover the contents as byte[] and wirte it as byte[] as per your sample code.
Yet, I haven't tried but I find the problem that for some number of characters it is not working, after some number of characters are passed in the file, it is read. Interesting, isn't it?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial