.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>
相关推荐
✎ ﹏梦醒͜ღ҉繁华落℘40 分钟前
WPF学习(四)
学习·wpf
✎ ﹏梦醒͜ღ҉繁华落℘1 小时前
WPF学习(动画)
学习·wpf
唐青枫4 小时前
C#.NET log4net 详解
c#·.net
ChaITSimpleLove15 小时前
使用 Dockerfile 构建基于 .NET9 的跨平台基础镜像
.net·dockerfile·.net aspire·dotnet-sdk·pwsh·docker image·docker buildx
weixin_4471035818 小时前
Wpf布局之Canvas面板!
wpf
葬歌倾城19 小时前
waferMap图像渲染
c#·wpf
甄天19 小时前
WPF路由事件:冒泡、隧道与直接全解析
c#·wpf·visual studio
专注VB编程开发20年20 小时前
C#,VB.NET从JSON数据里提取数组中的对象节点值
c#·json·.net
三千道应用题21 小时前
WPF学习笔记(12)下拉框控件ComboBox与数据模板
wpf