首页 > 编程语言 >C# WPF 将第三方DLL嵌入 exe

C# WPF 将第三方DLL嵌入 exe

时间:2023-08-12 10:04:16浏览次数:36  
标签:exe Assembly Name stream C# DLL assemblyRawBytes assemblyName path

没成功,只是做个记录,后面再研究

希望将第三方的 HandyControl.dll 嵌入到 exe 中,这样不用发多个文件给别人

C# WPF  将第三方DLL嵌入 exe_xml


将第三方DLL。加载到解决方案中

C# WPF  将第三方DLL嵌入 exe_App_02


添加引用

C# WPF  将第三方DLL嵌入 exe_xml_03

将“属性页”中的“复制本地”项改为“False”

C# WPF  将第三方DLL嵌入 exe_App_04


右键点击最开始复制到项目目录中的dll文件,点击“属性”。

将“属性”页中的“复制到输出目录”设置为“不复制”,“生成操作”设置为“嵌入的资源”。

C# WPF  将第三方DLL嵌入 exe_App_05

记事本编辑 VipReminder.csproj
下面增加如下代码

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

C# WPF  将第三方DLL嵌入 exe_App_06

在 App.xml.cs 中增加如下代码

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        AppDomain.CurrentDomain.AssemblyResolve += OnResolveAssembly;
    }

    private static Assembly OnResolveAssembly(object sender, ResolveEventArgs args)
    {
        Assembly executingAssembly = Assembly.GetExecutingAssembly();
        var executingAssemblyName = executingAssembly.GetName();
        var resName = executingAssemblyName.Name + ".resources";

        AssemblyName assemblyName = new AssemblyName(args.Name); string path = "";
        if (resName == assemblyName.Name)
        {
            path = executingAssemblyName.Name + ".g.resources"; ;
        }
        else
        {
            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);
        }
    }
}



标签:exe,Assembly,Name,stream,C#,DLL,assemblyRawBytes,assemblyName,path
From: https://blog.51cto.com/u_15116285/7056441

相关文章

  • 知识图谱(Knowledge Graph)- Neo4j 5.10.0 CentOS 安装
    系统需求版本JDKCPU内存硬盘Neo4j5.x17Intelx86-x64Corei3minimum,Corei7recommended.AMDx86-x64,MacARM.最低2GB,推荐16GB+10G+Neo4j5.x11Neo4j5.x8JDK17下载:https://www.oracle.com/java/technologies/downloads/#java17基于jar的......
  • 你们眼睛干涩,胀痛吗?C# WPF 久坐提醒桌面小程序 - 内附 眼肌运动、远视力表高清图
    目录说明设置提醒时间,及休息时间久坐提醒倒计时休息提醒倒计时休息到计时代码说明主窗体设置工作到计时休息倒计时源码高清图久坐提醒桌面小程序:干这行职业病比较多,之前用爱丽(即:玻璃酸钠滴眼液),用的时候挺舒服,缓解吧,不过治标不治本。注意休息,加强锻炼非常有必要,每工作1小时,休息10分......
  • 你们眼睛干涩,胀痛吗?C# WPF 久坐提醒桌面小程序 - 内附 眼肌运动、远视力表高清图
    目录说明设置提醒时间,及休息时间久坐提醒倒计时休息提醒倒计时休息到计时代码说明主窗体设置工作到计时休息倒计时源码高清图久坐提醒桌面小程序:干这行职业病比较多,之前用爱丽(即:玻璃酸钠滴眼液),用的时候挺舒服,缓解吧,不过治标不治本。注意休息,加强锻炼非常有必要,每工作1小时,休息10分......
  • docker 安装 Influxdb-relay 使用
    1.influxdbrelay的作用和特性数据转发:InfluxDBRelay可以从一个或多个源(input)接收数据,并将其转发到一个或多个目标(output)的InfluxDB实例中。负载均衡:Relay可以通过在多个目标实例之间分配请求来提供负载均衡,确保可伸缩性和高可用性。故障恢复:当某个目标实例发生故障时,Relay可......
  • 文心一言 VS 讯飞星火 VS chatgpt (74)-- 算法导论7.2 2题
    二、如果用go语言,当数组A的所有元素都具有相同值时,QUICKSORT的时间复杂度是什么?文心一言:在Go语言中,对一个所有元素都相等的数组进行快速排序(QuickSort)的时间复杂度是O(nlogn)。快速排序是一种分而治之的排序算法,它将一个数组分成两个子数组,然后对这两个子数组进行递归排序。在最......
  • 文心一言 VS 讯飞星火 VS chatgpt (73)-- 算法导论7.1 4题
    四、如何修改QUICKSORT,使得它能够以非递增序进行排序?文心一言:快速排序(Quicksort)是一种常用的排序算法,基于分治策略进行设计。默认情况下,快速排序会以递增序进行排序。若想修改快速排序以实现非递增排序,我们需要调整比较和交换的逻辑。以下是修改后的非递增快速排序的Python代码......
  • weblogic security
    HowtoDebugWLSUserSecurityInformation(DocID1513220.1)HowtoSetUpSecurityDebugintheWebLogicConsole(DocID2076131.1)WebLogicServerSecurityWarningsDetectedThroughtheAdminConsole(DocID2788605.1)解释WLS管理控制台(在应用2021......
  • Know Thy Complexities!
    https://www.bigocheatsheet.com/ KnowThyComplexities!Hithere! ThiswebpagecoversthespaceandtimeBig-OcomplexitiesofcommonalgorithmsusedinComputerScience. Whenpreparingfortechnicalinterviewsinthepast,Ifoundmyselfspendinghour......
  • antd vue 解决a-select下拉菜单跟随页面滚动
    问题描述:antd a-select下拉菜单会跟着滚动一块走 官方原话:getPopupContainer菜单渲染父节点。默认渲染到body上,如果你遇到菜单滚动定位问题,试试修改为滚动的区域,并相对其定位  解决方案:这样就ok了 :getPopupContainer="triggerNode=>triggerNode.parentNode"......
  • $count (aggregation)
    https://www.mongodb.com/docs/manual/reference/operator/aggregation/count/ $count(aggregation) Definition $count Passesadocumenttothenextstagethatcontainsacountofthenumberofdocumentsinputtothestage.NOTEDisambiguationThi......