Since all the other solutions are in C#, and I needed this for VB.NET, this includes clarification about where to insert the configuration change, the necessary imports, and the way to add a handler, instead of C#'s += syntax.
For any WPF application, not each project, the following needs to be added to make the code compile to a single EXE. It will still include the DLL’s in the output folder, but the EXE will contain all of them.
- Unload the WPF project (usually the view)
- Right-click the project and edit it
- In the document, paste the following code after this line
<Import Project="$(MSBuildToolsPath)\Microsoft.VisualBasic.targets" />
Code to paste
<Target Name="AfterResolveReferences">
<ItemGroup>
<EmbeddedResource Include="@(ReferenceCopyLocalPaths)" Condition="'%(ReferenceCopyLocalPaths.Extension)' == '.dll'">
<LogicalName>%(ReferenceCopyLocalPaths.DestinationSubDirectory)%(ReferenceCopyLocalPaths.Filename)%(ReferenceCopyLocalPaths.Extension)
</LogicalName>
</EmbeddedResource>
</ItemGroup>
</Target>
- Close it, save it, and then reload the project
- In the Application.xaml.vb file add the following code, or if something already exists in the file, add this to it:
Imports System.Reflection
Imports System.Globalization
Imports System.IO
Class Application
Public Sub New()
AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf OnResolveAssembly
End Sub
Private Shared Function OnResolveAssembly(ByVal sender As Object, ByVal args As ResolveEventArgs) As Assembly
Dim executingAssembly As Assembly = Assembly.GetExecutingAssembly()
Dim assemblyName As AssemblyName = New AssemblyName(args.Name)
Dim path = assemblyName.Name & ".dll"
If assemblyName.CultureInfo.Equals(CultureInfo.InvariantCulture) = False Then path = String.Format("{0}\{1}", assemblyName.CultureInfo, path)
Using stream As Stream = executingAssembly.GetManifestResourceStream(path)
If stream Is Nothing Then Return Nothing
Dim assemblyRawBytes = New Byte(stream.Length - 1) {}
stream.Read(assemblyRawBytes, 0, assemblyRawBytes.Length)
Return Assembly.Load(assemblyRawBytes)
End Using
End Function
End Class