DCSIMG
Back to Basics – Reading a File into Memory Stream - Gil Fink's Blog

Gil Fink's Blog

Fink about IT

News

Microsoft MVP

My Facebook Profile My Twitter Profile My Linkedin Profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2012 Gil Fink

Hebrew Articles

Index Pages

My OSS Projects

English Articles

Back to Basics – Reading a File into Memory Stream

Back to Basics – Reading a File into Memory Stream

Today I was asked to help a developer with a simple task she had. That task included reading an image file into a Back to Basics – Reading a File into Memory Streammemory stream in order to send the image through an e-mail attachment. This post will show you how to do exactly that.

Reading a File into Memory Stream

Here is the code for reading the file into a memory stream:

using (FileStream fileStream = File.OpenRead(filePath))
{
    MemoryStream memStream = new MemoryStream();
    memStream.SetLength(fileStream.Length);
    fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);
}

That’s it for the reading part. Pay attention to use the using statement in order to dispose the FileStream after you use it.

Adding a MemoryStream as Attachment to a MailMessage

In order to use a stream as an attachment for an e-mail message object all you have to do is to write the following code:

msg.Attachments.Add(new Attachment(memStream, filename, MediaTypeNames.Image.Jpeg)); 

In the code sample msg is an instance of a MailMessage class, memStream is the MemoryStream (such as the memory stream from the previous code sample) and filename is the name of the file in the attachment. Since I know that the image is jpeg then I use the MediaTypeNames.Image.Jpeg.

Summary

Reading a file into a stream is a very basic thing to know. In the post I showed how to read an image into a memory stream and also how to attach it into a mail message object.

Posted: Mar 03 2011, 11:47 AM by Gil Fink | with 5 comment(s) |
תגים:,

Comments

Michael Hamrah said:

Hey Gil- big fan of the blog, and always appreciate the back to basics- sometimes the simplest things to do are the hardest because people get out of practice.

I'd like to see this with Async I/O operations- even though it's (arguably) okay to stick to the vanilla sync methods, the .NET community needs to push into a "new basics" mindset- using async techniques to leverage multi-core cpu's and achieve a better level of user experience with .NET apps.  The Task Parallel Library makes this very simple!

# March 4, 2011 4:01 PM

Erik said:

The code for reading the file into a memory stream can be more terse like this:

var memoryStream = new MemoryStream(File.ReadAllBytes(filePath));

# March 6, 2011 6:43 PM

Gil Fink said:

@Erik,

Thanks for the code sample.

# March 7, 2011 8:14 AM

Gil Fink said:

@Michael,

Thanks for the comment and for reading my Blog!

As for your request, I added it to the list of future posts so ping me if it doesn't happen :-).

# March 7, 2011 8:17 AM

Shahar Eldad said:

Hi Gil

I believe it`s always a good practice to close the stream and afterward to dispose of it.

# March 8, 2011 9:11 AM