本文介绍: 3.发布时,如弹出以下错误:无法复制文件“……userProjectobjReleasenet8.0-windowswin-x86singlefilehost.exe”,原因是找不到该文件。需要在userProject.csproj中添加如下元素:。1.如果直接将.NetCore3.1升级为.net8,发布时可能会弹出如:不再需要使用Microsoft.NET.Sdk.WindowsDesktop SDK。请考虑将根项目元素的Sdk届性更改为“Microsoft.NET.Sdk”的错误。

在使用.NetCore3.1时,可以通过设置以下工程配置文本来将项目发布为一个单独的应用程序文件:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <UseWPF>true</UseWPF>
	  <PublishSingleFile>true</PublishSingleFile>
	  <RuntimeIdentifier>win-x86</RuntimeIdentifier>
	  <!--PublishTrimmed>true</PublishTrimmed-->
  </PropertyGroup>
 
</Project>

1.如果直接将.NetCore3.1升级为.net8,发布时可能会弹出如:不再需要使用Microsoft.NET.Sdk.WindowsDesktop SDK。请考虑将根项目元素的Sdk届性更改为“Microsoft.NET.Sdk”的错误。

将工程配置项目修改为:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <UseWPF>true</UseWPF>
	  <PublishSingleFile>true</PublishSingleFile>
	  <RuntimeIdentifier>win-x86</RuntimeIdentifier>
	  <!--PublishTrimmed>true</PublishTrimmed-->
  </PropertyGroup>
</Project>

2.发布为单个文件时的配置如下:
在这里插入图片描述
3.发布时,如弹出以下错误:无法复制文件“……userProjectobjReleasenet8.0-windowswin-x86singlefilehost.exe”,原因是找不到该文件。需要在userProject.csproj中添加如下元素:

<SelfContained>true</SelfContained>

4.此时生成的文件还不是最终的,同时生成的依赖还有vcruntime140_cor3.dll及wpfgfx_cor3.dll。若需要将这两个库依赖也包含进去,则还需要添加元素:

<IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>

同时,生成的独立文件体积也成倍增加。

5.裁剪体积:

只添加:

<PublishTrimmed>true</PublishTrimmed>

发布生成时报:
“启用剪裁时,不支持或不推荐使用 WPF。请转到 https://aka.ms/dotnet-illink/wpf 以了解详细信息。”

解决:
添加:

<_SuppressWpfTrimError>true</_SuppressWpfTrimError>

再次调试时出运行时错误:

NotSupportedException: Built-in COM has been disabled via a feature switch. See https://aka.ms/dotnet-illink/com for more information.

解决:

添加:

<BuiltInComInteropSupport>true</BuiltInComInteropSupport>

调试可以正常了。发布后运行,却不能正常启动。

解决:

添加:

<TrimMode>partial</TrimMode>

6.最终如下:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <UseWPF>true</UseWPF>
	  <PublishSingleFile>true</PublishSingleFile>
	  <RuntimeIdentifier>win-x86</RuntimeIdentifier>
	  <SelfContained>true</SelfContained>
      <PublishTrimmed>true</PublishTrimmed>
      <_SuppressWpfTrimError>true</_SuppressWpfTrimError>
      <BuiltInComInteropSupport>true</BuiltInComInteropSupport>
      <TrimMode>partial</TrimMode>
  </PropertyGroup>
</Project>

原文地址:https://blog.csdn.net/fancyang/article/details/135864471

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_62773.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注