How to Convert a Stream datatype to string.For example Dim path As System.IO.Stream = (Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(AssemblyName))
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
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)
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
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
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 = newMemoryStream(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.
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);
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.
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)
How to Convert Stream to String
Mark Vernon
-_Cypher_-
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.Keith Rome
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
PennyB
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.
kmassey
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 likehtmlPath="\Programfiles\Assemblyname\Sample.htm"
Guaw
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.
kolja2005
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
}
TaylorA
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.
RoviWil
Sorry Peter,
It is String to Stream;
Vijil jones
Bangalore.
Jeltz
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)
Sentinel3304
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.