.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>
相关推荐
亦陈不染6 小时前
WPF-MVVM的简单入门(第一个MVVM程序)
wpf
一个专注写bug的小白猿10 小时前
.net实现ftp传输文件保姆教程
后端·c#·.net
FuckPatience10 小时前
WPF TreeView 条目右键命令 ContextMenu按钮绑定命令
wpf
初级代码游戏13 小时前
winform迁移:从.net framework 到 .net9
.net·迁移·.net framework
TeamDev13 小时前
使用 Shadcn UI 构建 C# 桌面应用
前端·后端·.net
CodeCraft Studio13 小时前
CADSoftTools发布两款重要更新:CAD VCL Multiplatform 16.2 与 CAD .NET 16全新发布
.net·cad vcl·cad .net·cad文件格式解析·cad文件编辑·cad文件查看器
The 旺15 小时前
【案例实战】HarmonyOS分布式购物车:多设备无缝协同的电商体验
分布式·wpf·harmonyos
FuckPatience15 小时前
WPF MVVM下 ItemsControl条目命令绑定传参
wpf
唐青枫16 小时前
C#.NET FluentSqlKata 全面解析:基于链式语法的动态 SQL 构建
c#·.net
加号31 天前
【WPF】自定义颜色拾取器
wpf·自定义颜色拾取器