Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Stream to String
Message
 
To
30/01/2006 21:46:33
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
01091728
Message ID:
01091733
Views:
12
>>I want to convert an embedded resourse stream to a string. The following code does what I want it to:
>>
>>byte[] ba;
>>using (System.IO.Stream s = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(sResouce))
>>{
>>  ba = new byte[s.Length];
>>  s.Read(ba, 0, ba.Length);
>>}
>>
>>string myString = System.Text.ASCIIEncoding.ASCII.GetString(ba);
>>
>>
>>Is there a better way to do this? It just seems like a lot of work to first convert the Stream to a byte array and then convert it to a string.
>
>This is what I use to do a VFP FileToStr() equivalent:
>
>
>    ' FileToStr() VFP equivalent
>    ' expC1 File name
>    Public Shared Function FileToStr(ByVal tcFileName As String) As String
>        Dim loFile As IO.StreamReader
>        Dim lcString As String
>        Try
>            loFile = New IO.StreamReader(tcFileName, System.Text.Encoding.Default)
>            lcString = loFile.ReadToEnd()
>            loFile.Close()
>        Catch loError As Exception
>            Framework.App.ErrorSetup(loError)
>            lcString = ""
>        End Try
>        Return lcString
>    End Function
>
>
>Once the stream reader object is created, I have to do ReadToEnd() to get the content.


Thanks Michel. That was just what I needed. Translated into C# this is what my code looks like.
string myString = "";
using (System.IO.Stream s = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(sResouce))
{
  using (System.IO.StreamReader sr = new System.IO.StreamReader(s))
  {
    myString = sr.ReadToEnd();
  }
}
MessageBox.Show(myString);
A lot cleaner than what it looked like before.
Again thank you very much.

Sincerely,
Einar
Semper ubi sub ubi.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform