wpf dll和exe合并成一个新的exe

微软有一个工具叫ILMerge可以合并dll exe等,但是对于wpf的应用程序而言这个工具就不好用了。我的这方法也是从国外一个博客上找来的。仅供大家参考。

第一步:把下面的代码写到你的项目文件的标准的Microsoft.CSharp下面,也就是.csproj的文件里

<Target Name="AfterResolveReferences">
  <ItemGroup>
    <EmbeddedResource Include="@(ReferenceCopyLocalPaths)" Condition="'%(ReferenceCopyLocalPaths.Extension)' == '.dll'">
      <LogicalName>%(ReferenceCopyLocalPaths.DestinationSubDirectory)%(ReferenceCopyLocalPaths.Filename)%(ReferenceCopyLocalPaths.Extension)</LogicalName>
    </EmbeddedResource>
  </ItemGroup>
</Target>

  第二步:把你需要引用的dll复制到你当前项目的Resources文件夹下面,并把他们作为嵌入式资源(dll的属性里有此设置),为了避免文件名冲突,把这些dll的后缀加上 resources (System.Windows.Interactivity.resources.dll)

第三步:通常WPF应用程序包含一个xaml文件,它充当一个神奇的入口点到应用程序和启动的第一个窗口。然而,xaml其实没那么神奇。如果你仔细看一下这些文件夹在您的项目文件夹obj,你会发现一个appg.cs文件,这是来自你的xaml。它包含一个正常的“静态空隙主要“c#入口点所以为了得到在WPF之前,所有您需要做的就是定义自己的入口点进入一个新类,做你需要,然后调用正常WPF入口点。添加一个类Program.cs并把它设置成程序的入口点。

第四步:构建Program.cs的代码

public class Program
{
    [STAThreadAttribute]
    public static void Main()
    {
        AppDomain.CurrentDomain.AssemblyResolve += OnResolveAssembly;
        App.Main();
    }

    private static Assembly OnResolveAssembly(object sender, ResolveEventArgs args)
    {
        Assembly executingAssembly = Assembly.GetExecutingAssembly();
        AssemblyName assemblyName = new AssemblyName(args.Name);

        string path = assemblyName.Name + ".dll";
        if (assemblyName.CultureInfo.Equals(CultureInfo.InvariantCulture) == false)
        {
            path = String.Format(@"{0}\{1}", assemblyName.CultureInfo, path);
        }

        using (Stream stream = executingAssembly.GetManifestResourceStream(path))
        {
            if (stream == null)
                return null;

            byte[] assemblyRawBytes = new byte[stream.Length];
            stream.Read(assemblyRawBytes, 0, assemblyRawBytes.Length);
            return Assembly.Load(assemblyRawBytes);
        }
    }
}

  简单的四步,如果引用的dll中的版本有低于当前项目版本的话会有错误,我是直接把这个dll拿出来和合并后的exe放在一起了。或者把当前引用的dll版本统一掉。出现此问题最多的时候是引用的第三方的dll。

原文地址:http://www.digitallycreated.net/Blog/61/combining-multiple-assemblies-into-a-single-exe-for-a-wpf-application

原文地址:https://www.cnblogs.com/yhdkzy/p/3137392.html