none
How to Convert Stream to String

    Question

  • How to Convert a Stream datatype to string.For example

    Dim path As System.IO.Stream = (Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(AssemblyName))

    the path should to converted string.

    Saturday, September 10, 2005 12:26 PM

Answers

  • It depends on the content of the stream whether or not you can conver to a string.

    What type of embedded resource are you trying to load from the assembly's resources with GetManifestResourceStream?  If you're trying to load textual data, let's say you've embedded a resource from file "notes.txt" you would use the Stream's Read method to get the data as bytes then Encoding.ASCII to convert it to a string:


    Dim path As System.IO.Stream = (Reflection.Assembly. _
        GetExecutingAssembly().GetManifestResourceStream("vbnewsgrouptesting.notes.txt"))
    Dim bytes(path.Length) As Byte
    path.Position = 0
    path.Read(bytes, 0, path.Length)
    Dim data As String = Encoding.ASCII.GetString(bytes)
    Debug.WriteLine(data)

     

    Saturday, September 10, 2005 2:21 PM

All replies

  • It depends on the content of the stream whether or not you can conver to a string.

    What type of embedded resource are you trying to load from the assembly's resources with GetManifestResourceStream?  If you're trying to load textual data, let's say you've embedded a resource from file "notes.txt" you would use the Stream's Read method to get the data as bytes then Encoding.ASCII to convert it to a string:


    Dim path As System.IO.Stream = (Reflection.Assembly. _
        GetExecutingAssembly().GetManifestResourceStream("vbnewsgrouptesting.notes.txt"))
    Dim bytes(path.Length) As Byte
    path.Position = 0
    path.Read(bytes, 0, path.Length)
    Dim data As String = Encoding.ASCII.GetString(bytes)
    Debug.WriteLine(data)

     

    Saturday, September 10, 2005 2:21 PM
  • Hi Peter,
         Thanks for ur reply and ur sample  shows how to load the contant.
    i am using a html file as embedded resource file.
    so if i  use the above code it gives the source code of html file.But i needs to load
    the html page in webbrowser like

    WebBrowser.Navigate("file://" & htmlPath)

    and htmlpath should be like

    htmlPath="\Programfiles\Assemblyname\Sample.htm"

     

     




    Monday, September 12, 2005 5:22 AM
  • Do you want to call WebBrowser.Navigate after the call to GetManifestResourceStream()?

    If that's the case, just write the stream out to a temporary file and call Navigate; there's no need to fiddle with encoding.  Something like:

    System.IO.Stream inStream = (Assembly.GetExecutingAssembly().GetManifestResourceStream("WindowsApplication2.HTMLPage1.htm"));
    string path = Path.GetTempFileName();
    FileStream fs =
    new FileStream(path, FileMode.Truncate);
    BinaryWriter w =
    new BinaryWriter(fs);
    byte [] bytes = new byte[inStream.Length];
    inStream.Position = 0;
    inStream.Read(bytes, 0, (
    int)inStream.Length);
    w.Write(bytes);
    w.Flush();
    w.Close();
    Debug.WriteLine("Output " + bytes.Length.ToString() + " bytes to " + path);
    // TODO: WebBrowser.Navigate("file://" + path);

     


    Change Path.GetTempFileName() to something that generates a file path that you want.
    Monday, September 12, 2005 2:40 PM
  • Hi Peter,
         Your Previous sample

    Dim path As System.IO.Stream = (Reflection.Assembly. _
        GetExecutingAssembly().GetManifestResourceStream("vbnewsgrouptesting.notes.txt"))
    Dim bytes(path.Length) As Byte
    path.Position = 0
    path.Read(bytes, 0, path.Length)
    Dim data As String = Encoding.ASCII.GetString(bytes)

      
    Works fine when i assign  to webbrowser  like this.

    WebBrowser1.DocumentText = data

    Thank you.

    Tuesday, September 13, 2005 10:16 AM
  •  

    I want to convert string to string

    I am trying like this

    string str= " Welcome to c# world";

    Stream st;

     

    st = str;

     

    when doing this I Got error.

    So i try like this

    st =(stream) str;

    Then also getting error;

     

    Please help me;

     

    Vijil

    Bangalore.

     

     

     

     

    Friday, September 29, 2006 12:50 PM
  • Hi Vijil.  Can you be more specific?  You say you want to convert a String to a String, then you show code that users a String and a Stream.  Do you want to serialize a string?
    Friday, September 29, 2006 1:40 PM
  • Sorry Peter,

                           It is String to Stream;

     

    Vijil jones

    Bangalore.

    Friday, September 29, 2006 1:44 PM
  • Hi Vijil.  It depends on what you want to do and what type of stream it is.  You don't "convert" a string to a stream, you usually serialize a string to a stream.  For example:

                String text = "0123456789";

                using (Stream stream = new MemoryStream(100))

                {

                    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

                    binaryFormatter.Serialize(stream, text);

                }

    ...where you'd replace MemoryStream with a stream that probably persists to a storage device other than memory.

    Saturday, September 30, 2006 1:24 AM
  • Dear Peter,

     

                         Thanks for the Quick replay and the solution. I  am very happy. I search the google and tryed myself past one day.

    Your answer is great. Your two line coding reduce my tension. Really  you are great.

     

    Now I am trying to convert the text file to pdf file. Could you tell me where I want post my query and get the result, and I am excepting some URL from you.

     

    Vijil

    Banalore.

    Saturday, September 30, 2006 5:53 AM
  • Faced with the same problem, needing to get a string onto a stream, I found your posting and tried your method.  It did not work for me and I was unable to figure out why.  (I am very new to .net stuff.)  The solution I ended up with seems a little clearer and simpler:

        String text = "0123456789";
        using (Stream stmText = new MemoryStream(text.Length))
        {
            StreamWriter swText = new StreamWriter(stmText);

            swText.Write(text);
            swText.Flush();

            stmText.Position = 0;

           // Use the stream
        }

    Monday, April 30, 2007 7:38 PM
  • converting a Stream to String
    Was this post helpful ?

    Hi everyone, I'm having the same problem trying to convert a stream to String. But nothing seems to work. Here's my code:

     

    void displayImage(IO:tream^ imagePath) {

    array<Byte>^bytes = gcnew array<Byte>((int)imagePath->Length);

    imagePath->Position=0;

    int n = imagePath->Read(bytes,0,(int) imagePath->Length);

    System:tring^ path = System::Text::ASCIIEncoding::GetString(bytes);

    }

     

     

    but I'm getting the error :

     

    error C2352: 'System::Text::Encoding::GetString' : illegal call of non-static member function

    c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see declaration of 'System::Text::Encoding::GetString'

     

    Does anyone know what I'm doing wrong. I've been spending about 2 days to convert the stream I get from my OpenFileDialog to string to show it. I've also tried

    System:tring^ path = System::Text::UnicodeEncoding::GetString(bytes);

     

    but I get the same error.

     

    I would appritiate any help.

     

    Thanks

    Friday, January 25, 2008 11:23 AM
  • Seems I am upping qute old topic..
    But maybe it will help somebody :-)

    You should use
    System:tring^ path = System::Text::UnicodeEncoding->GetString(bytes);
    instead code that you wrote.

    Wednesday, July 16, 2008 9:34 AM