.net framework wpf 打包免安装exe文件

1、打开所在项目csproj文件添加以下内容:

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

2、App.xmal.cs类添加以下代码

cs 复制代码
 public partial class App : Application
 {
     public App()
     {
         AppDomain.CurrentDomain.AssemblyResolve += OnResolveAssembly;
     }

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

         var path = assemblyName.Name + ".dll";
         if (assemblyName.CultureInfo.Equals(CultureInfo.InvariantCulture) == false)
             path = $@"{assemblyName.CultureInfo}\{path}";
         using var stream = executingAssembly.GetManifestResourceStream(path);
         if (stream == null) return null;
         var assemblyRawBytes = new byte[stream.Length];
         stream.Read(assemblyRawBytes, 0, assemblyRawBytes.Length);
         return Assembly.Load(assemblyRawBytes);
     }
 }

3、打包删除exe以外的文件,需要在所在项目csproj文件添加以下内容

cs 复制代码
<Target Name="DeleteOtherFile" AfterTargets="AfterBuild">
        <ItemGroup>
            <OtherFiles Include="$(OutputPath)\*" Exclude="$(OutputPath)\$(AssemblyName).exe" />
        </ItemGroup>
        <Delete Files="@(OtherFiles)" />
    </Target>
相关推荐
用户7227868123445 小时前
.NET 实现雪花算法:高效生成分布式唯一 ID
.net
玩泥巴的6 小时前
.NET 8+ 飞书API实战:自动化群组管理与消息推送
c#·.net·二次开发·飞书
唐青枫8 小时前
C#.NET 范围与索引(Range、Index)完全解析:语法、用法与最佳实践
c#·.net
许泽宇的技术分享21 小时前
当AI Agent遇上.NET:微软Agent Framework的架构奥秘与实战启示
人工智能·microsoft·.net
SEO-狼术1 天前
DevExpress DXperience Crack
.net
咩图1 天前
WPF+Prism8.0.0.1909+C#创建一个桌面程序
c#·wpf·prism
我是唐青枫1 天前
一文理解 C#.NET Tuples:从基础到高级应用
c#·.net
雁于飞1 天前
分布式基础
java·spring boot·分布式·spring·wpf·cloud native
oioihoii1 天前
WPF入门指南:解析默认项目结构
wpf
缺点内向1 天前
C# 中 Word 文档目录的插入与删除指南
开发语言·c#·word·.net