Welcome to MSDN Blogs Sign in | Join | Help

You had me at "Hello World!"

XAML, WPF, Silverlight, .NET, Office 2007, Windows code samples and other interesting things

Syndication

Reading Embedded Resources

Just a small reminder for myself. I have to do this way too often recently to remember.

This is how to read embedded resources in Silverlight application (or WPF app for that matter) - like binaries, images, xml, etc, from an embedded resource

        /// <summary>
        /// Reads information from an embedded resource.
        /// In VS, set the type of the file in solution explorer to "Embedded Resource"
        /// <example>
        /// bytes = ReadBytesFromStream("MyTestProgram.SomeDataFile.xml")
        /// </example>
        /// </summary>
        /// <param name="streamName"></param>
        /// <returns></returns>
        private byte[] ReadBytesFromStream(string streamName)
        {
            using (System.IO.Stream stream = this.GetType().Assembly.GetManifestResourceStream(streamName))
            {
                byte[] result = new byte[stream.Length];
                stream.Read(result, 0, (int) stream.Length);
                return result;
            }
        }

Published Wednesday, May 14, 2008 10:37 AM by nikola

Filed under: , , ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
Page view tracker