馃惀note.

灏忛偿銇∕icrosoft <3 銇偍銉炽偢銉嬨偄銇妧琛揃log馃摎

/p:PublishSingleFile=true銇otnet publish銇椼仧銇ㄣ亶銇儜銈广伄鎵便亜

dotnet publish -c Release -r win10-x64 /p:PublishSingleFile=true銇у崢涓銉曘偂銈ゃ儷銇櫤琛屻倰闅涖伀鑷垎鑷韩(Exe)銇儜銈广伄鍙栧緱銇с伅銇俱仯銇熻┍銇с仚銆

Single Binary鍖

dotnet publish銇欍倠闅涖伀PublishSingleFileRuntimeIdentifier銈掓寚瀹氥仚銈嬨亾銇ㄣ仹鍗樹竴銇瓻xe銈掑嚭鍔涖仹銇嶃伨銇欍

publish銈炽優銉炽儔 渚

dotnet publish -c Release -r win10-x64 /p:PublishSingleFile=true

銇俱仧銇痗sproj銇互涓嬨伄鍐呭銈掕杓夈仐銇dotnet publish -c Release銇椼仸銈傚悓妲樸仹銇欍

.csproj

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
+    <PublishSingleFile>true</PublishSingleFile>
+    <RuntimeIdentifier>win10-x64</RuntimeIdentifier>
  </PropertyGroup>

  </Project>

鍗樹竴銇瓻xe銇ㄣ亜銇c仸銈傚疅闅涖伀銇疊uild銉曘偐銉儉鍐呫伄DLL銇ㄣ亱銈掑湩绺仐銇︿繚鎸併仐銆丒xe瀹熻鏅傘伀瑙e噸銇椼仸銈嬨仩銇戙仯銇姐亜锛

瀹熻銇椼仸銇裤倠

publish銇椼仧Exe銈掑疅琛屻仚銈嬨仺%USERPROFILE%/AppData/Local/Temp/.net/浠ヤ笅銇偄銉椼儶銈便兗銈枫儳銉虫瘞銇儠銈┿儷銉銇岀敓鎴愩仌銈屻倠銆

瑭︺仐銇互涓嬨伄銈炽兗銉夈伄Exe銈扨ublish銇椼佸疅琛屻仐銇︺伩銈嬨

Path 妞滆銈炽兗銉

using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;

namespace PublishSingleFileSample
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine($"GetCallingAssembly:\t{Path.GetDirectoryName(Assembly.GetCallingAssembly().Location)}");
            Console.WriteLine($"GetEntryAssembly:\t{Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)}");
            Console.WriteLine($"GetExecutingAssembly:\t{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}");
            Console.WriteLine($"GetCurrentProcess:\t{Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName)}");
            Console.WriteLine($"GetCurrentDirectory:\t{Directory.GetCurrentDirectory()}");
            Console.WriteLine($"CurrentDirectory:\t{Environment.CurrentDirectory}");
        }
    }
}

瀹熻銇椼仧绲愭灉銇互涓嬨伄閫氥倞銇с仚銆

瀹熻绲愭灉

PS C:\Users\piyosi> .\work\PublishSingleFileSample.exe
GetCallingAssembly:     C:\Users\piyosi\AppData\Local\Temp\.net\PublishSingleFileSample\32lboto0.c0l
GetEntryAssembly:       C:\Users\piyosi\AppData\Local\Temp\.net\PublishSingleFileSample\32lboto0.c0l
GetExecutingAssembly:   C:\Users\piyosi\AppData\Local\Temp\.net\PublishSingleFileSample\32lboto0.c0l
GetCurrentProcess:      C:\Users\piyosi\work
GetCurrentDirectory:    C:\Users\piyosi
CurrentDirectory:       C:\Users\piyosi

Assembly绯汇仹鍙栧緱銇椼仧銉戙偣銇睍闁嬨仌銈屻仧鍏堛伄銉戙偣銈掔ず銇椼仸銇勩伨銇欍 Process銇嬨倝鍙栧緱銇椼仧銉戙偣銇敓鎴愩仐銇烢xe銇儜銈广倰绀恒仐銇︺亜銇俱仚銇

涓婅銇樊銇倛銈PublishSingleFile銈掓寚瀹氥仚銈嬪墠寰屻仹銈€儣銉伄鎸欏嫊銇屽銈忋仯銇︺仐銇俱仯銇︺佸ぇ澶夊洶銇c仧鎬濄亜銈掋仐銇俱仐銇熴傘

浠ヤ笂銇с仚銆