Hi,
I'm writing a WinForms app in C# which requires a 3rd party executable
called htmldoc.exe in order to create PDF documents from HTML files. To
prevent the app from not functioning if the user accidentally deletes this
file, I'm investigating the possibility of making it an Embedded Resource.
When the app starts up, it will search for the executable and "extract" it
if it's missing. I found the following code on the Net:
using Microsoft.Win32;
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
byte[] abytFile;
Assembly objAssembly = Assembly.GetExecutingAssembly();
Stream objStream = objAssembly.GetManifestResourceStream("htmldoc.exe ");
abytFile = new Byte[objStream.Length]; // this line errors
objStream.Read(abytFile, 0, (int)objStream.Length);
FileStream objFileStream = new
FileStream(Path.GetDirectoryName(objAssembly.Locat ion) + "\\htmldoc.exe",
FileMode.Create);
objFileStream.Write(abytFile, 0, (int)objStream.Length);
objFileStream.Close();
This compiles with no errors, but errors on the line I've indicated. The
error string is:
System.NullReferenceException: Object reference not set to an instance of an
object.\r\n at HBQ.frmMDI.frmMDI_Load(Object sender, EventArgs e) in
c:\\documents and settings\\mrae\\my documents\\visual studio
projects\\hbq\\hbq\\frmmdi.cs:line 851
which indicates that the Stream has not been created correctly - if I hover
the cursor over the objStream variable, it shows "<undefined value>"
I'm slightly puzzled as to what I'm doing wrong - the executable is
definitely there and has been added to the project with its Build Action set
to Embedded Resource.
Any assistance gratefully received.
Best regards,
Mark Rae