title: Unraveling the confusion about Embedded Resources
body: <p><strong>EDIT: Read answer number 1 from Tim Schmelter and then use this question for examples of how to embed resources and access them at runtime.</strong></p>
  
  <p>The subject of embedded resources comes up a lot, especially with people asking how to access the embedded files at runtime. Things get more confusing because Visual Studio gives you 2 different ways of embedding a resource, and different ways of accessing those resources at runtime. The problem is that depending on which method you used to embed the resource, the method you’re trying to use to access the file at runtime might not work. This post is an attempt to clear up all the confusion that I see out there, but I also have a question that nobody can seem to answer factually: Why is the size of my compiled program TWICE the size of the embedded resource (sometimes)? For example if I embed a 20MB file into my project, why does my program compile to 40MB? I haves asked this question in the past and nobody was able to reproduce my results. I found that the reason they were not able to reproduce was because they were embedding the file in a different way. See here:</p>
  
  <p>Method 1:</p>
  
  <p>Double-click on My Project to open the property pages and go to the Resources Tab. Now click Add Resource > Add Existing File. Browse to the file you want to embed. For this example I’m using an executable. You will now see your file on the Resources Tab:</p>
  
  <p><img src="https://i.stack.imgur.com/leFt1.jpg" alt="enter image description here"></p>
  
  <p>You will also see that a folder named Resources was created under your project and the embedded file has been placed in this folder:</p>
  
  <p><img src="https://i.stack.imgur.com/8Dts1.jpg" alt="enter image description here"></p>
  
  <p><strong>EDIT: THIS NEXT STEP WAS THE PROBLEM. TURNS OUT THAT WHEN YOU ADD A FILE VIA THE RESOURCES TAB YOU SHOULD <em>NOT</em> SET THE BUILD ACTION TO EMBEDDED RESOURCE.</strong> Counter intuitive to say the least!</p>
  
  <p>Now with the file selected, look down at the properties window for the file and change the build action to Embedded Resource: <strong>(this step should ONLY be performed when you add a file via method 2).</strong></p>
  
  <p><img src="https://i.stack.imgur.com/lot1P.jpg" alt="enter image description here"></p>
  
  <p>Now compile your program. You will see that the size of your compiled program is at least double the size of your embedded resource. This does not happen with method 2. See here:</p>
  
  <p>Method 2:</p>
  
  <p>Right-click on your project name and choose Add > Existing Item. Browse to your file, and this time you will notice that while it was indeed placed under your project, there was no Resources folder created:</p>
  
  <p><img src="https://i.stack.imgur.com/jauSC.jpg" alt="enter image description here"></p>
  
  <p>Now once again select the file and change the Build Action to Embedded Resource and compile. This time the size of the compiled program will be as you expected - about the size of the embedded file and not double the size as with method 1.</p>
  
  <p>Which method you use to embed your file will determine which method you can use to access the file at runtime. For method 1 this is very simple, all you have to do is:</p>
  
  <pre><code>My.Computer.FileSystem.WriteAllBytes(Path, My.Resources.ResourceName, Append)
  </code></pre>
  
  <p>Where Path is the location and name for the file you want to save on the harddrive, ResourceName is the name of the embedded resource that you see in the project window (minus any extension), and Append is whether or not you want to create a new file or overwrite an existing file. So for example, using test.exe from the above images, I could save that file to the C drive like this:</p>
  
  <pre><code>My.Computer.FileSystem.WriteAllBytes(“C:\test.exe”, My.Resources.test, False)
  </code></pre>
  
  <p>Couldn’t be easier.</p>
  
  <p>Method 2 however doesn’t appear to give you access to My.Resources so it gets a little more complicated. You have to create a Stream to hold the resource, put the stream into a byte array, then write the bytes out to the file system. The simplest way I have found to do this is like this:</p>
  
  <pre><code>Using s As Stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(Project.ResourceName)
  Dim bytes(s.Length) As Byte
  s.Read(bytes, 0, bytes.Length)
  File.WriteAllBytes(OutputFile, bytes)
  End Using
  </code></pre>
  
  <p>With this method ResourceName must contain the file extension AND project name so using our example from above we can just do:</p>
  
  <pre><code>Using s As Stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(WindowsApplication1.test.exe)
  Dim bytes(s.Length) As Byte
  s.Read(bytes, 0, bytes.Length)
  File.WriteAllBytes(“C:\test.exe”, bytes)
  End Using
  </code></pre>
  
  <p>Text-based files are a little different:</p>
  
  <pre><code>Dim output As String
  Using sr As StreamReader = New StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(WindowsApplication1.test.txt))
  output = sr.ReadToEnd()
  End Using
  
  Using sw As StreamWriter = New StreamWriter(“C:\test.txt”)
  sw.Write(output)
  End Using
  </code></pre>
  
  <p>Having struggled with this in the past I hope this will help someone. And if you think you can explain factually why method 1 of embedding a resource bloats my compiled program to double its size, I would really appreciate it.</p>