After sitting and wondering just exactly how I could extract needed DLL files into
the program directory and several searches of finding people saying it could not
be done, I found a way to do it.
The first step is to find the DLL or resource you want/need to include in your project.
Find the file and drag it into your VB.NET Solution or right click on your form in the
solution explorer -> Add... -> Add Existing Item... and in the Files of type drop down
box select All Files (*.*) and navigate to the file you want to include, highlight it and
click on Open.
You will now see the file in your Solution Explorer. Click on the file and in the properties
window select Embedded Resource in the Build Action drop down box.
Ok so now we have the file included in our project, but just how do we get it out before
the program uses the needed resource? Well all we have to do is have the file output
to the directory before it is called upon, and this usually has to be done before the program
fully loads up . First before we get into details, you must know the
forms name. You can
find this out by looking at the Solution explorer and the name in bold
text is your Form name
such as Form or MyProject. You will need to know this in the upcoming section.
Click the + next to Windows Form Designer generated code to expand the code.
You should see a section called:
Public Sub New()
For example here
my Project is called Embed and I'm wanting to output a needed Winsock dll:
Public Sub New()
'Get our needed .DLL
GetResource(Application.StartupPath & "\", "AxInterop.MSWinsockLib.dll", "Embed.AxInterop.MSWinsockLib.dll")
'This call is required by the Windows Form Designer.
'Add any initialization after the InitializeComponent() call
I made a function for use of doing this which you place normally in your project after the windows form designer code:
Private Function GetResource(ByVal Dir As String, ByVal StrFile As String, ByVal Resource As String)
If IO.File.Exists(Dir & StrFile) = False Then
Dim output As New IO.FileStream(Dir & StrFile, IO.FileMode.Create, IO.FileAccess.Write)
Dim buffer(System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(Resource).Length - 1) As Byte
System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(Resource).Read(buffer, 0, System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(Resource).Length)
output.Write(buffer, 0, System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(Resource).Length)
And it's as easy as that when using the call to the function. I hope this tutorial will help many
when needing to include DLL's with their files or to make sure the file is there when the program
starts like if a user was to delete the DLL this would check if it is there and extract it if need be.