用于编译的#if语句中的.NET框架未标识net461



我有两个控制台应用程序。一个在.Net Core 2中,一个在Net 461 中

我正在尝试确定我正在运行的框架

在这种情况下,我能够识别.net核心,但它无法确定它我的.net 461应用

我需要为.net框架添加额外信息吗

这是我所拥有的:

#if NET461
Console.WriteLine("hello from net NET461");  // expecting this to execute, but it does not
#endif
#if NETCOREAPP2_0
Console.WriteLine("hello from net Core");
#endif
#if NETSTANDARD2_0
Console.WriteLine("hello from net NETSTANDARD2_0");
#endif

更新

虽然我没有得到我想要的结果。我真的只需要知道它是否在运行.net核心。所以我只做了以下

#if NETCOREAPP2_0
Console.WriteLine("hello from .Net Core");
#else
Console.WriteLine("hello from Not .Net Core");
#endif

感谢的所有回复

我发现csproj的结构也很重要。这对我有效。

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net461</TargetFramework>
</PropertyGroup>
</Project>

最新更新