If you’re true intent is to simply give a user one executable assuming the correct .NET Framework is installed then why not simply embed the DLL/assembly as an embedded resource and allow .NET to load the DLL/assembly if it is missing. Why hide this file if you have the rights to use it?
The attached solution has two projects. The first project is a simple class library with a single class Customer. The second Windows form project references the class library and creates a list of customers, which are displayed in a DataGridView.
If the class library DLL is not located when your application requires it the following code will extract the DLL from the primary assembly and load it.
To test it out, build the solution then remove the DLL from the Windows form project directory followed by running the Windows form executable. The DLL will be extracted from the code in ResolveEventHandler. Of course instead of the logic in ResolveEventHandler you could find other methods but I think this works well.
Hook into AssemblyResolve, find the customer dll and load it.
Support Name of the DLL which is embeddedCode:Public Sub New() InitializeComponent() AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf ResolveEventHandler End Sub Function ResolveEventHandler(ByVal sender As Object, ByVal args As ResolveEventArgs) As Assembly Dim ExecutingAssemblies As Assembly = Assembly.GetExecutingAssembly() Dim ReferencedAssmbNames() As AssemblyName = ExecutingAssemblies.GetReferencedAssemblies() Dim AssemblyName As AssemblyName Dim MyAssembly As Assembly = Nothing For Each AssemblyName In ReferencedAssmbNames 'Look for the assembly names that have raised the "AssemblyResolve" event. If (AssemblyName.FullName.Substring(0, AssemblyName.FullName.IndexOf(",")) = args.Name.Substring(0, args.Name.IndexOf(","))) Then Dim TempAssemblyPath As String TempAssemblyPath = IO.Path.Combine(Application.StartupPath, args.Name.Substring(0, args.Name.IndexOf(",")) & ".dll") My.Resources.CustomerDemo.FileSave(My.Application.CustomerDllFileName) Application.DoEvents() 'Load the assembly from the specified path. MyAssembly = Assembly.LoadFrom(TempAssemblyPath) Exit For End If Next Return MyAssembly End Function
Used to extract embedded DLLCode:Namespace My Partial Friend Class MyApplication Public ReadOnly Property CustomerDllFileName() As String Get Return IO.Path.Combine(My.Application.Info.DirectoryPath, "CustomerDemo.dll") End Get End Property End Class End Namespace
ReferencesCode:<System.Runtime.CompilerServices.Extension()> _ Public Sub FileSave(ByVal BytesToWrite() As Byte, ByVal FileName As String) If IO.File.Exists(FileName) Then IO.File.Delete(FileName) End If Dim FileStream As New System.IO.FileStream(FileName, System.IO.FileMode.OpenOrCreate) Dim BinaryWriter As New System.IO.BinaryWriter(FileStream) BinaryWriter.Write(BytesToWrite) BinaryWriter.Close() FileStream.Close() End Sub
ResolveEventHandler Delegate
Unload Assemblies From an Application Domain




Reply With Quote