Link to home
Start Free TrialLog in
Avatar of johnywhite
johnywhite

asked on

Convert Byte Array to Stream

I need to convert a Byte array to a Stream. How can I do it?
Avatar of william007
william007

Code from here:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=937187&SiteID=1

// using System.IO

 

// read an example file to new byte array

Byte [] exampleByteArray1 = File.ReadAllBytes(@"C:\boot.ini");

// write data to the new stream

MemoryStream mStream = new MemoryStream();

mStream.Write(exampleByteArray1,0,exampleByteArray1.Length);

 

// read another example file to new byte array

Byte[] exampleByteArray2 = File.ReadAllBytes(@"C:\boot.ini");

// append new data to the stream

mStream.Write(exampleByteArray2,0,exampleByteArray2.Length);

 

// set Position of stream to 0 for read data from the first position

mStream.Position = 0;

// read data from stream and write it to an example file.

try

{

FileStream fileStream = File.Open(@"C:\temp.txt",FileMode.CreateNew);

mStream.WriteTo(fileStream);

fileStream.Close();

}

catch(Exception ex)

{

// using System.Windows.Forms

MessageBox.Show(ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error,MessageBoxDefaultButton.Button1,MessageBoxOptions.ServiceNotification);

}

finally

{

mStream.Close();

}
ASKER CERTIFIED SOLUTION
Avatar of pollock_d
pollock_d

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial