为了调试,您可以使用DLL文件。您可以使用dotnet ConsoleApp2.dll
运行它。如果您想生成EXE文件,则必须生成自包含应用程序。
要生成自包含应用程序(在Windows中为EXE),必须指定目标运行时(特定于您针对的操作系统)。
仅适用于.NET Core 2.0之前的版本:首先,在.csproj文件中添加目标运行时的运行时标识符(支持的RID列表):
<PropertyGroup>
<RuntimeIdentifiers>win10-x64;ubuntu.16.10-x64</RuntimeIdentifiers>
</PropertyGroup>
从.NET Core 2.0开始,不再需要上述步骤。
然后,在发布应用程序时设置所需的运行时:
dotnet publish -c Release -r win10-x64
dotnet publish -c Release -r ubuntu.16.10-x64
更新:2022年2月
.NET 6仍然适用以下内容。
.NET 5的更新!
以下内容适用于/在2020年11月.NET 5正式发布之后。
(请参见下面的快速术语部分,不仅仅是操作说明)
dotnet publish --output "{any directory}" --runtime {runtime}
--configuration {Debug|Release} -p:PublishSingleFile={true|false}
-p:PublishTrimmed={true|false} --self-contained {true|false}
例子:
dotnet publish --output "c:/temp/myapp" --runtime win-x64 --configuration Release
-p:PublishSingleFile=true -p:PublishTrimmed=true --self-contained true
* 上述两种情况下,最新的.NET5 SDK将自动安装在您的PC上。
输入任何文件夹位置,然后点击“完成”
点击“发布”
目标运行时
查看RID列表
部署模式
启用ReadyToRun编译
TLDR:这是.Net5的Ahead of Time Compilation(AOT)等价物。预编译为本地代码,应用程序通常会更快地启动。应用程序的性能更高(或者不是!),取决于许多因素。更多信息在此处
修剪未使用的程序集
当设置为true时,dotnet将生成非常精简和小巧的.exe文件,并且仅包括必要的内容。请小心。例如:当在应用程序中使用反射时,您可能不希望将此标志设置为true。
--configuration
标志,它才对我起作用。 - Shad<PropertyGroup>
<Temp>$(SolutionDir)\packaging\</Temp>
</PropertyGroup>
<ItemGroup>
<BootStrapFiles Include="$(Temp)hostpolicy.dll;$(Temp)$(ProjectName).exe;$(Temp)hostfxr.dll;"/>
</ItemGroup>
<Target Name="GenerateNetcoreExe"
AfterTargets="Build"
Condition="'$(IsNestedBuild)' != 'true'">
<RemoveDir Directories="$(Temp)" />
<Exec
ConsoleToMSBuild="true"
Command="dotnet build $(ProjectPath) -r win-x64 /p:CopyLocalLockFileAssemblies=false;IsNestedBuild=true --output $(Temp)" >
<Output TaskParameter="ConsoleOutput" PropertyName="OutputOfExec" />
</Exec>
<Copy
SourceFiles="@(BootStrapFiles)"
DestinationFolder="$(OutputPath)"
/>
</Target>
我在这里提供了一个示例:https://github.com/SimonCropp/NetCoreConsole
如果可以使用 .bat 文件,您可以创建与 DLL 文件同名的 bat 文件(并将其放置在同一文件夹中),然后粘贴以下内容:
dotnet %~n0.dll %*
c:\> "path\to\batch\file" -args blah
这是我的hacky解决方法 - 生成一个控制台应用程序(.NET Framework),它读取自己的名称和参数,然后调用dotnet [nameOfExe].dll [args]
。
当然,这假设目标机器上已安装.NET。
以下是代码。随意复制!
using System;
using System.Diagnostics;
using System.Text;
namespace dotNetLauncher
{
class Program
{
/*
If you make .NET Core applications, they have to be launched like .NET blah.dll args here
This is a convenience EXE file that launches .NET Core applications via name.exe
Just rename the output exe to the name of the .NET Core DLL file you wish to launch
*/
static void Main(string[] args)
{
var exePath = AppDomain.CurrentDomain.BaseDirectory;
var exeName = AppDomain.CurrentDomain.FriendlyName;
var assemblyName = exeName.Substring(0, exeName.Length - 4);
StringBuilder passInArgs = new StringBuilder();
foreach(var arg in args)
{
bool needsSurroundingQuotes = false;
if (arg.Contains(" ") || arg.Contains("\""))
{
passInArgs.Append("\"");
needsSurroundingQuotes = true;
}
passInArgs.Append(arg.Replace("\"","\"\""));
if (needsSurroundingQuotes)
{
passInArgs.Append("\"");
}
passInArgs.Append(" ");
}
string callingArgs = $"\"{exePath}{assemblyName}.dll\" {passInArgs.ToString().Trim()}";
var p = new Process
{
StartInfo = new ProcessStartInfo("dotnet", callingArgs)
{
UseShellExecute = false
}
};
p.Start();
p.WaitForExit();
}
}
}
dotnet [nameOfExe].dll %*
命令呢?这样可以更加方便地运行程序。 - Chet
dotnet <path>.dll
回答了我的问题,我还怀疑其他许多人也是这样。 (我没有考虑清楚,一直在尝试dotnet run <path>.dll
,显然是行不通的)!(经过反思,最好关闭此问题,支持另一个具有类似答案集的问题)。 - Ruben Bartelink