.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>
相关推荐
专注VB编程开发20年3 小时前
C#全面超越JAVA,主要还是跨平台用的人少
java·c#·.net·跨平台
一个帅气昵称啊13 小时前
.Net通过EFCore和仓储模式实现统一数据权限管控并且相关权限配置动态生成
.net·efcore·仓储模式
helloworddm15 小时前
CalculateGrainDirectoryPartition
服务器·c#·.net
步步为营DotNet16 小时前
深度剖析.NET中HttpClient的请求重试机制:可靠性提升与实践优化
开发语言·php·.net
ChaITSimpleLove17 小时前
使用 .net10 构建 AI 友好的 RSS 订阅机器人
人工智能·.net·mcp·ai bot·rss bot
专注VB编程开发20年17 小时前
vb.net宿主程序通过统一接口直接调用,命名空间要一致
服务器·前端·.net
ChaITSimpleLove1 天前
基于 .NET Garnet 1.0.91 实现高性能分布式锁(使用 Lua 脚本)
分布式·.net·lua
用户4488466710602 天前
.NET进阶——深入理解线程(2)Thread入门到精通
c#·.net
没有bug.的程序员2 天前
SOA、微服务、分布式系统的区别与联系
java·jvm·微服务·架构·wpf·日志·gc