Welcome to NavWin!
  

How to extract an embedded resource

The following code snippit will extract an embedded resource file from a project and write it to a file.

public static void WriteResourceToFile(Assembly targetAssembly, string resourceName, string filepath)

        {

            using (Stream s = targetAssembly.GetManifestResourceStream(targetAssembly.GetName().Name + "." + resourceName))

            {

                if (s == null)

                {

                    throw new Exception("Cannot find embedded resource '" + resourceName + "'");

                }

                byte[] buffer = new byte[s.Length];

                s.Read(buffer, 0, buffer.Length);

                using (BinaryWriter sw = new BinaryWriter(File.Open(filepath, FileMode.Create)))

                {

                    sw.Write(buffer);

                }

            }

        }

 

Note that if you embed the resource using the ‘Embedded Resource’ property that you will find on all files in the project as shown in the screenshot below

 

Then the resource name will take the name of the default namespace of the project.

 

If you put the resource file in a sub folder then you define the path like a namespace e.g. if the resource file is in the sub folder EmbeddedResources/WordFiles then the full resource path would be:

NavWin.Apps.WordToWeb.EmbeddedResources.WordFiles.EmptyWordDocument.DocX