Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Reflection;
using System.Runtime;

namespace HowToDetermineDotNetVersionProgramatically;
Expand All @@ -7,8 +6,8 @@ public static class DotNetCoreVersion
{
public static string GetNetCoreVersion()
{
var assembly = typeof(GCSettings).GetTypeInfo().Assembly;
var assemblyPath = assembly.Location.Split(new[] {'/', '\\'}, StringSplitOptions.RemoveEmptyEntries);
var assembly = typeof(GCSettings).Assembly;
var assemblyPath = assembly.Location.Split(['/', '\\'], StringSplitOptions.RemoveEmptyEntries);
var netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");

if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public static class DotNetRuntimeVersion
{
public static string GetDotNetRuntimeVersionsInstalled()
{
var process = new Process();
using var process = new Process();
process.StartInfo.FileName = "dotnet";
process.StartInfo.Arguments = "--list-runtimes";
process.StartInfo.RedirectStandardOutput = true;
Expand All @@ -20,7 +20,7 @@ public static string GetDotNetRuntimeVersionsInstalled()
{
return GetDotNetRuntimeVersionsInstalled()
.Split(Environment.NewLine)
.Select(x => x.Split(' '))
.Select(x => x.Split(' ', 3))
.Where(x => x.Length == 3)
.Select(x => (name: x[0], version: x[1], installPath: x[2]))
.ToArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public static class DotNetSdkVersion
{
public static string GetDotNetSdkVersionsInstalled()
{
var process = new Process();
using var process = new Process();
process.StartInfo.FileName = "dotnet";
process.StartInfo.Arguments = "--list-sdks";
process.StartInfo.RedirectStandardOutput = true;
Expand All @@ -19,7 +19,7 @@ public static string GetDotNetSdkVersionsInstalled()
{
return GetDotNetSdkVersionsInstalled()
.Split(Environment.NewLine)
.Select(x => x.Split(' '))
.Select(x => x.Split(' ', 2))
.Where(x => x.Length == 2)
.Select(x => (version: x[0], installPath: x[1]))
.ToArray();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
using static HowToDetermineDotNetVersionProgramatically.DotNetRuntimeVersion;
using static HowToDetermineDotNetVersionProgramatically.DotNetSdkVersion;

var path = "HowToDetermineDotNetVersionProgramaticallyDemo.dll";
Console.WriteLine(GetAssemblyDotNetVersion(path));

Console.WriteLine(GetTargetFrameworkName());

Console.WriteLine(GetEnvironmentVersion());

Console.WriteLine(GetFrameworkDescription());
Expand All @@ -20,64 +15,39 @@
var versionPart = parts[1];
Console.WriteLine(versionPart);

Console.WriteLine(GetTargetFrameworkName());

var path = Path.Combine(AppContext.BaseDirectory, "HowToDetermineDotNetVersionProgramaticallyDemo.dll");
Console.WriteLine(GetAssemblyDotNetVersion(path));

Console.WriteLine(GetNetCoreVersion());

Console.WriteLine(GetDotNetSdkVersionsInstalled());
static ICollection<(string version, string installPath)> GetInstalledSdkVersionsCollection()
{
return GetDotNetSdkVersionsInstalled()
.Split(Environment.NewLine)
.Select(x => x.Split(' '))
.Where(x => x.Length == 2)
.Select(x => (version: x[0], installPath: x[1]))
.ToArray();
}
foreach (var (sdkVersion, installPath) in GetInstalledSdkVersionsCollection())
foreach (var (sdkVersion, installPath) in GetInstalledSdkVersions())
{
Console.WriteLine($"SDK version: {sdkVersion} Install path: {installPath}");
}

Console.WriteLine(GetDotNetRuntimeVersionsInstalled());
static ICollection<(string name, string version, string installPath)>
GetInstalledDotNetRuntimeVersionsCollection()
{
return GetDotNetRuntimeVersionsInstalled()
.Split(Environment.NewLine)
.Select(x => x.Split(' '))
.Where(x => x.Length == 3)
.Select(x => (name: x[0], version: x[1], installPath: x[2]))
.ToArray();
}
foreach (var (name, version, installPath) in GetInstalledDotNetRuntimeVersionsCollection())
foreach (var (name, version, installPath) in GetInstalledDotNetRuntimeVersions())
{
Console.WriteLine($"Runtime name: {name} Version: {version} Install path: {installPath}");
}

Console.ReadLine();

// output
// 7.0.0.0
// .NETCoreApp,Version=v7.0
// 7.0.0
// .NET 7.0.0
// output on .NET 10.0.10, Windows
// 10.0.10
// .NET 10.0.10
// .NET
// 7.0.0
// 7.0.0

// ...
// 7.0.100 [/usr/local/share/dotnet/sdk]
// 10.0.10
// .NETCoreApp,Version=v10.0
// 10.0.0.0
// 10.0.10
// ...

// ...
// SDK version: 7.0.100 Install path: [/usr/local/share/dotnet/sdk]
// 10.0.302 [C:\Program Files\dotnet\sdk]
// ...

// SDK version: 10.0.302 Install path: [C:\Program Files\dotnet\sdk]
// ...
// Microsoft.NETCore.App 7.0.1 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
// ...

// Microsoft.NETCore.App 10.0.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
// ...
// Runtime name: Microsoft.NETCore.App
// Version: 7.0.1
// Install path: [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
// Runtime name: Microsoft.NETCore.App Version: 10.0.10 Install path: [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
// ...
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
namespace HowToDetermineDotNetVersionProgramaticallyTests;

public class DotNetAssemblyVersionLiveTests
public class DotNetAssemblyVersionTests
{
[Fact]
public void GivenPathToDllBuiltWithDotNet_WhenCallingGetAssemblyDotNetVersion_ThenReturnExpectedVersion()
{
// Given
var path = typeof(DotNetAssemblyVersionLiveTests).Assembly.Location;
var expectedVersion = "7.0";
var path = typeof(DotNetAssemblyVersionTests).Assembly.Location;
var expectedVersion = "10.0";

// When
var actualVersion = DotNetAssemblyVersion.GetAssemblyDotNetVersion(path);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
namespace HowToDetermineDotNetVersionProgramaticallyTests;

public class DotNetClrVersionLiveTests
public class DotNetClrVersionTests
{
[Fact]
public void GivenAssemblyBuiltWithDotNet_WhenCallingGetEnvironmentVersion_ThenReturnVersion()
{
// Given
var expectedVersion = "7.0";
var expectedVersion = "10.0";

// When
var actualVersion = DotNetClrVersion.GetEnvironmentVersion();
Expand All @@ -19,7 +19,7 @@ public void GivenAssemblyBuiltWithDotNet_WhenCallingGetEnvironmentVersion_ThenRe
public void GivenPathToDllBuiltWithDotNet_WhenCallingGetAssemblySupportedMinimumClrVersion_ThenReturnExpectedVersion()
{
// Given
var path = typeof(DotNetClrVersionLiveTests).Assembly.Location;
var path = typeof(DotNetClrVersionTests).Assembly.Location;
var expectedVersion = "v4.0";

// When
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
namespace HowToDetermineDotNetVersionProgramaticallyTests;

public class DotNetCoreVersionLiveTests
public class DotNetCoreVersionTests
{
[Fact]
public void GivenCurrentProcessGarbageCollectionSettings_WhenCallingGetNetCoreVersion_ThenReturnExpectedVersion()
{
// Given
var expectedVersion = "7.0";
var expectedVersion = "10.0";

// When
var actualVersion = DotNetCoreVersion.GetNetCoreVersion();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
namespace HowToDetermineDotNetVersionProgramaticallyTests;

public class DotNetFrameworkDescriptionLiveTests
public class DotNetFrameworkDescriptionTests
{
[Fact]
public void GivenAssemblyBuiltWithDotNet_WhenCallingGetFrameworkDescription_ThenReturnDescription()
{
// Given
var expectedDescription = ".NET 7.";
var expectedDescription = ".NET 10.";

// When
var actualDescription = DotNetFrameworkDescription.GetFrameworkDescription();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace HowToDetermineDotNetVersionProgramaticallyTests;

public class DotNetRuntimeVersionLiveTests
public class DotNetRuntimeVersionTests
{
[Fact]
public void
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace HowToDetermineDotNetVersionProgramaticallyTests;

public class DotNetSdkVersionLiveTests
public class DotNetSdkVersionTests
{
[Fact]
public void GivenPlatformWithInstalledDotNetSdks_WhenCallingGetDotNetSdkVersionsInstalled_ThenReturnInstalledSdks()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace HowToDetermineDotNetVersionProgramaticallyTests;

public class DotNetTargetFrameworkNameLiveTests
public class DotNetTargetFrameworkNameTests
{
/* for use in library build on .NET Framework
[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand All @@ -10,13 +10,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.11.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="coverlet.collector" Version="10.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Loading