title: How To Get A Stream Object From A Resource File (Console App/Windows Service Project)
body: <p>I'm creating a Windows service and am trying to access some files I added to a resource file, but I'm stuck because I don't know how to access the individual files. Just for some background info, here's what I've done so far:</p>
  
  <ul>
  <li><p>This is a C# Windows Service application running in debug mode as a console app, which helps me step into the code.</p></li>
  <li><p>I added a resource file to the root called "Resources.resx".</p></li>
  <li><p>In my resource file, I added a few jpg images and html files using the visual designer/editor.</p></li>
  <li><p>After I added the images and html files to the resource file, a new folder in my project appeared named "Resources" with all the files I added.</p></li>
  <li><p>In this new folder, I went to the properties of each file and changed the Build Action to Embedded Resource. (I don't know if this is necessary. Some blog I searched said to try it.)</p></li>
  <li><p>The project's namespace is called "MicroSecurity.EmailService".</p></li>
  <li><p>In order to get the name of the resource file, I used</p>
  
  <p>GetType().Assembly.GetManifestResourceNames()</p></li>
  </ul>
  
  <p>and I get the following</p>
  
  <p>GetType().Assembly.GetManifestResourceNames()   {string[2]} string[]
      [0] "MicroSecurity.EmailService.Services.EmailService.resources"    string
      [1] "MicroSecurity.EmailService.Resources.resources"    string</p>
  
  <p>From this I identified that "MicroSecurity.EmailService.Resources.resources" is the string I want to use (index 1).</p>
  
  <ul>
  <li><p>I used the this code to get a stream object.</p>
  
  <p>var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MicroSecurity.EmailService.Resources.resources");</p></li>
  </ul>
  
  <p>When I add a watch to this variable during debugging, I can see things such as metadata for my images and etc.</p>
  
  <p>Here is where I'm stuck. I would like to access the image called "logo.jpg". This is what I'm doing to get at the image, but it's not working.</p>
  
  <pre><code>var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MicroSecurity.EmailService.Resources.resources.logo.jpg");
  </code></pre>
  
  <p>How can I get a stream from my logo.jpg file?</p>
  
  <p>UPDATE:</p>
  
  <p>Thanks to Andrew, I was able to figure it out. Below is some code I wrote for a demo project in order to study how the resource file works vs. embedding files directly. I hope this helps others to clarify the differences.</p>
  
  <pre><code>using System;
  using System.Drawing;
  using System.IO;
  using System.Reflection;
  
  namespace UsingResourceFiles
  {
      public class Program
      {
          /// &lt;summary&gt;
          /// Enum to indicate what type of file a resource is.
          /// &lt;/summary&gt;
          public enum FileType
          {
              /// &lt;summary&gt;
              /// The resource is an image.
              /// &lt;/summary&gt;
              Image,
  
              /// &lt;summary&gt;
              /// The resource is something other than an image or text file.
              /// &lt;/summary&gt;
              Other,
  
              /// &lt;summary&gt;
              /// The resource is a text file.
              /// &lt;/summary&gt;
              Text,           
          }
  
          public static void Main(string[] args)
          {
              // There are two ways to reference resource files:
              // 1. Use embedded objects.
              // 2. Use a resource file.
  
              // Get the embedded resource files in the Images and Text folders.
              UseEmbeddedObjects();
  
              // Get the embedded resource files in the Images and Text folders. This allows for dynamic typing
              // so the resource file can be returned either as a stream or an object in its native format.
              UseEmbeddedObjectsViaGetResource();
  
              // Use the zombie.gif and TextFile.txt in the Resources.resx file.
              UseResourceFile();
          }
  
          public static void UseEmbeddedObjects()
          { 
              // =============================================================================================================================
              //
              //                                                     -=[ Embedded Objects ]=-
              //
              // This way is the easiest to accomplish. You simply add a file to your project in the directory of your choice and then 
              // right-click the file and change the "Build Action" to "Embedded Resource". When you reference the file, it will be as an 
              // unmanaged stream. In order to access the stream, you'll need to use the GetManifestResourceStream() method. This method needs
              // the name of the file in order to open it. The name is in the following format:
              //
              // Namespace + Folder Path + File Name
              //
              // For example, in this project the namespace is "UsingResourceFiles", the folder path is "Images" and the file name is 
              // "zombie.gif". The string is "UsingResourceFiles.Images.zombie.gif". 
              //
              // For images, once the image is in a stream, you'll have to convert it into a Bitmap object in order to use it as an Image
              // object. For text, you'll need to use a StreamReader to get the text file's text.
              // =============================================================================================================================
              var imageStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("UsingResourceFiles.Images.zombie.gif");
              var image = new Bitmap(imageStream);
  
              var textStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("UsingResourceFiles.Text.TextFile.txt");
              var text = new StreamReader(textStream).ReadToEnd();
          }
  
          public static void UseEmbeddedObjectsViaGetResource()
          {
              // =============================================================================================================================
              //
              //                                             -=[ Embedded Objects Using GetResource() ]=-
              //
              // Using the overloaded GetResource() method, you can easily obtain an embedded resource file by specifying the dot file path
              // and type. If you need the stream version of the file, pass in false to the useNativeFormat argument. If you use the
              // GetResource() method outside of this file and are getting a null value back, make sure you set the resource's "Build Action"
              // to "Embedded Resource".
              // =============================================================================================================================
  
              // Use the GetResource() methods to obtain the Images\zombie.gif file and the text from the Text\TextFile.txt file.
              Bitmap image = GetResource("Images.zombie.gif", FileType.Image);
              Stream imageStream = GetResource("Images.zombie.gif", FileType.Image, false);
  
              string text = GetResource("Text.TextFile.txt", FileType.Text);
              Stream textStream = GetResource("Text.TextFile.txt", FileType.Text, false);
          }
  
          public static void UseResourceFile()
          {
              // =============================================================================================================================
              //
              //                                                      -=[ Resource File ]=-
              //
              // This way takes more upfront work, but referencing the files is easier in the code-behind. One drawback to this approach is
              // that there is no way to organize your files in a folder structure; everything is stuffed into a single resource blob.
              // Another drawback is that once you create the resource file and add any files to it, a folder with the same name as your
              // resource file is created, creating clutter in your project. A final drawback is that the properties of the Resources object
              // may not follow proper C# naming conventions (e.g. "Resources.funny_man" instead of "Resources.FunnyMan"). A plus for using
              // resource files is that they allow for localization. However, if you're only going to use the resource file for storing files,
              // using the files as embedded objects is a better approach in my opinion.
              // =============================================================================================================================
  
              // The Resources object references the resource file called "Resources.resx".
              // Images come back as Bitmap objects and text files come back as string objects.
              var image = Resources.zombie;
              var text = Resources.TextFile;
          }
  
          /// &lt;summary&gt;
          /// This method allows you to specify the dot file path and type of the resource file and return it in its native format.
          /// &lt;/summary&gt;
          /// &lt;param name="dotFilePath"&gt;The file path with dots instead of backslashes. e.g. Images.zombie.gif instead of Images\zombie.gif&lt;/param&gt;
          /// &lt;param name="fileType"&gt;The type of file the resource is.&lt;/param&gt;
          /// &lt;returns&gt;Returns the resource in its native format.&lt;/returns&gt;
          public static dynamic GetResource(string dotFilePath, FileType fileType)
          {
              try
              {
                  var assembly = Assembly.GetExecutingAssembly();
                  var assemblyName = assembly.GetName().Name;
                  var stream = assembly.GetManifestResourceStream(assemblyName + "." + dotFilePath);
                  switch (fileType)
                  { 
                      case FileType.Image:                    
                          return new Bitmap(stream);
                      case FileType.Text:
                          return new StreamReader(stream).ReadToEnd();
                      default:
                          return stream;
                  }
              }
              catch (Exception e)
              {
                  Console.WriteLine(e);
                  return null;
              }
          }
  
          /// &lt;summary&gt;
          /// This method allows you to specify the dot file path and type of the resource file and return it in its native format.
          /// &lt;/summary&gt;
          /// &lt;param name="dotFilePath"&gt;The file path with dots instead of backslashes. e.g. Images.zombie.gif instead of Images\zombie.gif&lt;/param&gt;
          /// &lt;param name="fileType"&gt;The type of file the resource is.&lt;/param&gt;
          /// &lt;param name="useNativeFormat"&gt;Indicates that the resource is to be returned as resource's native format or as a stream.&lt;/param&gt;
          /// &lt;returns&gt;When "useNativeFormat" is true, returns the resource in its native format. Otherwise it returns the resource as a stream.&lt;/returns&gt;
          public static dynamic GetResource(string dotFilePath, FileType fileType, bool useNativeFormat)
          {
              try
              {
                  if (useNativeFormat)
                  {
                      return GetResource(dotFilePath, fileType);
                  }
  
                  var assembly = Assembly.GetExecutingAssembly();
                  var assemblyName = assembly.GetName().Name;
                  return assembly.GetManifestResourceStream(assemblyName + "." + dotFilePath);
              }
              catch (Exception e)
              {
                  Console.WriteLine(e);
                  return null;
              }
          }
      }
  }
  </code></pre>