Click here to Skip to main content
15,880,405 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
hello,

I have a problem with converting a stream to string. The below given is the code I have used...
C#
public String GetStringfromStream(Stream strLogContent)
        {
            StreamReader sr = new StreamReader(strLogContent);
            String strQuery = sr.ReadToEnd();
            sr.Dispose();
            return strQuery;
        }

But it throws an error message Stream was not readable.
The exception is thrown from second line..("String strQuery = sr.ReadToEnd();")
Can anyone help me on this?

Regards
Sebastian
Posted

C#
public String GetStringfromFile(string filePath)
{
    using(StreamReader sr = new StreamReader(filePath))
    {           
       return sr.ReadToEnd();
    } 
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Mar-12 2:49am    
That is a perfect code sample, but I voted 4. Why? Because it does not address the OP's problem (or may or may not address it). If the stream strLogContent was of the right type and in the right mode, OP's code would work as well. How do you know OP needs the file, not anything else? The root of the problem could be before calling of OP's method.
--SA
Sergey Alexandrovich Kryukov 22-Mar-12 2:55am    
Anyway, I added my own answer where I explained it and offered an alternative, please see.

I credited your answer as your sample could work perfectly if OP really wants to work with the file.
--SA
Dean Oliver 22-Mar-12 7:09am    
I agree with you. I just gave an example on a file path because the question did not give enough information on what type of stream is being passed to the method etc.
public String GetStringfromStream(Stream strLogContent)
{
using (StreamReader sr = new StreamReader(strLogContent))
{
return sr.ReadToEnd();
}
}
 
Share this answer
 
Comments
Dean Oliver 22-Mar-12 2:23am    
please remember to place code in code tags.
Sebastian T Xavier 22-Mar-12 2:32am    
I still have the same issue...I am trying to use it in a class library..
First of all, is it all about logging? If so, don't do those streams. Use the class System.Diagnostics.EventLog; please see:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^].

For advanced uses (including uses of a file), please see my past answers:
MsBuild OutPut to the TextBox on the fly in Windows Application[^],
How to create event log under a folder[^].

Now, about the problem with the stream you have. Your code is not quite correct (you are not using exception handling which could be done via the using statement, as Dean have demonstrated), but the problem is not in the code you have shown. The problem is the actual run-time type and mode of the stream you had to create before calling your method. To see the problem, I would need to see how you construct this object. If you can use with file, the answer by Dean provides a perfect code sample.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900