Skip to content

Commit 8699905

Browse files
committed
feat: add DOTNET_ROOT env config for ApplicationHelper.GetDotnetPath
1 parent b3b28be commit 8699905

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/WeihanLi.Common/Helpers/ApplicationHelper.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,24 @@ public static LibraryInfo GetLibraryInfo(Assembly assembly)
6363
/// </summary>
6464
public static string? GetDotnetPath()
6565
{
66+
var environmentOverride = Environment.GetEnvironmentVariable("DOTNET_ROOT");
67+
if (!string.IsNullOrEmpty(environmentOverride) && Directory.Exists(environmentOverride))
68+
{
69+
var execFileName =
70+
#if NET6_0_OR_GREATER
71+
OperatingSystem.IsWindows()
72+
#else
73+
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
74+
#endif
75+
? "dotnet.exe"
76+
: "dotnet"
77+
;
78+
var dotnetExePath = Path.Combine(environmentOverride, execFileName);
79+
if (File.Exists(dotnetExePath))
80+
return dotnetExePath;
81+
82+
throw new InvalidOperationException($"dotnet executable file not found under specified DOTNET_ROOT {environmentOverride}");
83+
}
6684
return ResolvePath("dotnet");
6785
}
6886

@@ -73,11 +91,6 @@ public static string GetDotnetDirectory()
7391
{
7492
return environmentOverride;
7593
}
76-
environmentOverride = Environment.GetEnvironmentVariable("DOTNET_ROOT");
77-
if (!string.IsNullOrEmpty(environmentOverride))
78-
{
79-
return environmentOverride;
80-
}
8194

8295
var dotnetExe = GetDotnetPath();
8396

0 commit comments

Comments
 (0)