Ads 468x60px

Wednesday, January 24, 2018

How to use Embedded Resources in .NET Applications


Today we are going to present how to use embedded files in Visual Studio.


  1. Add your file into your project by right click on the project icon, then click on Add => Add Existing Item.
  2. Go to file properties, and choose the option "Build Action" to "Embedded Resrouce"
  3. Copy the following function that retrieve resources as Stream:
    Public Function GetFileFromResource(ByVal FileName As String, ByVal ProjectName As String) As IO.Stream
    Dim assmbly As Reflection.Assembly = Reflection.Assembly.GetExecutingAssembly()
    Dim stream As IO.Stream = assmbly.GetManifestResourceStream(ProjectName + "." + FileName )
     Return stream
    End Function
  4. Now you have a stream connection with the resource that you want to deal with.

Exampe:
If you have a sample project "TestProj" with the embedded resource file "Capture.PNG"
Then you call the function as : GetFileFromResource("Capture.PNG", "TestProj")