Compartilhar via


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;
            }
        }