Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Stream to String
Message
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
01091728
Message ID:
01091733
Vues:
10
>>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.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform