- We will create a .NET Core console application in Visual Studio 2017. Under Visual C# templates, select .NET Core and a Console App (.NET Core) project:
- When you created your console application, the code will look as follows:
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
- Modify your code to look as follows:
static void Main(string[] args)
{
Console.WriteLine("I can run on Windows, Linux and macOS");
GetSystemInfo();
Console.ReadLine();
}
private static void GetSystemInfo()
{
var osInfo = System.Runtime.InteropServices.RuntimeInformation.OSDescription;
Console.WriteLine($"Current OS is: {osInfo}");
}
- The method GetSystemInfo() just returns the current operating system the console application currently runs on. The csproj file for my application looks as follows:
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)$(MSBuildToolsVersion)
Microsoft.Common.props" />
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="***.cs" />
<EmbeddedResource Include="***.resx" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.App">
<Version>1.1.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.NET.Sdk">
<Version>1.0.0-alpha-20161104-2</Version>
<PrivateAssets>All</PrivateAssets>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)Microsoft.CSharp.targets" />
</Project>
The <version> is defined as 1.1.0.
If you are still running Visual Studio 2017 RC, it would be a good idea to check your installed NuGet packages to see whether there is an update available for your .NET Core version from .NET Core 1.0 to .NET Core 1.1.