1

Visual Studio 2022 C# WPF Project

I want to distribute a single .exe file, without requiring the target computer to install .NET 6.0 runtimes.

I can only find solutions for C/C++ code generation in the project's property window, but what I see in VS 2022 is totally different and there's no settings for static linking.

wangyiwei
  • 29
  • 1
  • 3
  • 1
    Check [this article](https://learn.microsoft.com/en-us/dotnet/core/deploying/#publish-self-contained). Just beware the reality that you then become responsible for ensuring updates to the core components. – DonBoitnott Feb 21 '22 at 17:58

1 Answers1

1

How to Deploy a single-file .NET 6 WPF app:

  • Right-click on the WPF application project in the Solution Explorer and select Edit Project File

  • Add the following line to the <PropertyGroup> element in the .csproj file:

    <IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
    
  • Right-click on the project in the Solution Explorer and select Publish

  • Set the Deployment mode to Self-contained under the Target Runtime settings

  • Select win-* as the Target runtime

  • Check the Produce single file option under File publish options

  • Click on Save and then on Publish

  • Copy the contents of the Target location to the target machine and run the app`

.csproj:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <OutputType>WinExe</OutputType>
        <TargetFramework>net6.0-windows</TargetFramework>
        <Nullable>enable</Nullable>
        <UseWPF>true</UseWPF>
        <IncludeNativeLibrariesForSelfExtract >true</IncludeNativeLibrariesForSelfExtract>
    </PropertyGroup>

</Project>

FolderProfile.pubxml:

<?xml version="1.0" encoding="utf-8"?>
<!--
  https://go.microsoft.com/fwlink/?LinkID=208121. 
  -->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Configuration>Release</Configuration>
    <Platform>Any CPU</Platform>
    <PublishDir>bin\Release\net6.0-windows\publish\win-x64\</PublishDir>
    <PublishProtocol>FileSystem</PublishProtocol>
    <TargetFramework>net6.0-windows</TargetFramework>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <SelfContained>true</SelfContained>
    <PublishSingleFile>True</PublishSingleFile>
    <PublishReadyToRun>False</PublishReadyToRun>
  </PropertyGroup>
</Project>
mm8
  • 163,881
  • 10
  • 57
  • 88
  • I still got multiple output files ``` D3DCompiler_47_cor3.dll PenImc_cor3.dll PresentationNative_cor3.dll vcruntime140_cor3.dll wpfgfx_cor3.dll [project_name].exe [project_name].pdb ``` with the following configuration: – wangyiwei Feb 25 '22 at 07:19
  • FolderProfile.pubxml ``` Release Any CPU bin\Release\net6.0-windows\publish\win-x64\ FileSystem net6.0-windows win-x64 true true true ``` – wangyiwei Feb 25 '22 at 07:19
  • [Project].csproj ``` WinExe net6.0-windows enable true x64 full true ``` – wangyiwei Feb 25 '22 at 07:19
  • See my edit for an example of a working configuration and compare it to yours. – mm8 Feb 28 '22 at 15:06
  • 1
    IncludeAllContentForSelfExtract is usually not a great idea; it extracts *all* DLLs to the filesystem which can be slow and take up more disk space. IncludeNativeLibrariesForSelfExtract should be good enough to get those last DLLs included in the exe. See here for more info: https://learn.microsoft.com/en-us/dotnet/core/deploying/single-file/overview#including-native-libraries – Reilly Wood Mar 02 '22 at 07:00
  • @ReillyWood: That's a very good point. I replaced `IncludeAllContentForSelfExtract` with `IncludeNativeLibrariesForSelfExtract` in my answer. The rest is the same. – mm8 Mar 02 '22 at 12:44
  • My publish generates two .pdb files, which looks like one for each project namespace. Are these files important? The executable runs fine without them. – Denis G. Labrecque May 31 '23 at 15:15
  • No, they contain debugging information: https://stackoverflow.com/questions/3899573/what-is-a-pdb-file – mm8 Jun 01 '23 at 09:29