1

I have 2 console apps. One in .Net Core 2 and one in .Net 461

I am trying to determine what framework I am running

In the case I am able to identify the the .net core but it is not able to determine it for my .net 461 app

Do I need to add additional information for the .net framework

Here is what I have:

#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

Update

Although I didn't get the results I wanted\expected. I was really only needed to know if it is running .net core or not. So I just did the following

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

Thanks for all the responses

H20rider
  • 2,162
  • 5
  • 29
  • 47
  • _but it is not working for my .net 461 app_ It would help if you could provide more detail about what "not working" means. Ideally you would provide a [mcve] and an indication of the output the example produces and an indication of the output desired. See [ask] for more tips on asking effective questions. – Frank Boyne Oct 27 '18 at 22:13
  • What do you get as output and what value did you expect to get? – Scott Chamberlain Oct 27 '18 at 22:16
  • What does `Environment.Version` return? What does https://learn.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed#net_d return? – mjwills Oct 27 '18 at 22:17
  • @ScottChamberlain Console.WriteLine("hello from net NET461"); – H20rider Oct 27 '18 at 22:21
  • 4.0.30319.42000 – H20rider Oct 27 '18 at 22:23
  • 1
    Possible duplicate of [Is it possible to conditionally compile to .NET Framework version?](https://stackoverflow.com/questions/1449925/is-it-possible-to-conditionally-compile-to-net-framework-version) – mjwills Oct 27 '18 at 22:25
  • @H20rider that is what you expect to run, but what is actually run, none of them or is it one of the other two options showing up? – Scott Chamberlain Oct 27 '18 at 22:37
  • @ScottChamberlain I was expecting to see "hello from net NET461" – H20rider Oct 27 '18 at 22:51
  • 1
    @You are ***still*** not answering my question, When you run it do you get nothing at all or do you get a message other than "hello from net NET461"? – Scott Chamberlain Oct 27 '18 at 23:12
  • @ScottChamberlain I am not. – H20rider Oct 28 '18 at 22:36
  • Conditional compilation only works if your project has multiple target frameworks. – Lex Li Nov 04 '18 at 17:34

1 Answers1

-1

I have found that the structure of the csproj matters as well. This worked for me.

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net461</TargetFramework>
  </PropertyGroup>

</Project>
  • Please explain how this matters? Isn't that the standard generated one? – Hille Apr 07 '21 at 14:46
  • In Visual Studio 2019 when I create a new project "Console App (.Net Framework)" I don't get this type of csproj format. I get something which contains this line v4.6.1 In this case, the #if NET461 doesn't work. – Adrian Ignat Apr 08 '21 at 15:07