.Net won't let you include a dll as a resource. The way around this is to push your dll into an image and include the image in the exe. If you skip the image header .net won't complain. You just need to mark the beginning and end of the dll binary data so you know where to start and stop when extracting it.
Here is a program I wrote that makes it easy to push your file into an image.
http://www.securitysoftware.cc/Programs/FileMerge.exe
Once you have done this you can use the following methods to save the image to a file and then extract the dll from the image.
//Save image to a file
PictureBox.Image.Save("C:\\temp_file");
//Extract the dll
ExtractFile("C:\\temp_file", "C:\\MyDll.dll", "^^^^^^^^^^", "**********");
//This method extracts the dll from the image file and saves it with the name given
public static bool ExtractFile(string sourceFile, string destination, string startMarker, string endMarker)
{
int filestart = 0;
int fileend = 0;
int marker_position = 0;
FileInfo finfo = new FileInfo(sourceFile);
FileStream fs = new FileStream(sourceFile, FileMode.Open);
//Create Buffer
byte[] buff = new byte[finfo.Length];
//Read file contents
fs.Read(buff, 0, (int)finfo.Length);
//Close file
fs.Close();
//Get file start position
for(int bytecnt = 0; bytecnt < buff.Length; bytecnt++)
{
//Look for startMarker
while (buff[bytecnt + marker_position] == startMarker[marker_position++])
{
if (marker_position == startMarker.Length)
{
filestart = bytecnt + marker_position;
break;
}
}
marker_position = 0;
}
//Find end position
for (int bytecnt = filestart; bytecnt < buff.Length; bytecnt++)
{
//Look for startMarker
while (buff[bytecnt + marker_position] == endMarker[marker_position++])
{
if (marker_position == startMarker.Length)
{
fileend = bytecnt;
break;
}
}
marker_position = 0;
}
//Save to file
fs = new FileStream(destination, FileMode.Create);
fs.Write(buff, filestart, fileend - filestart);
fs.Close();
return File.Exists(destination);
}
|