When I run dotnet --info
, among other information I receive also:
Runtime Environment:
...
Base Path: C:\Program Files\dotnet\sdk\1.0.0
Is there any way to get this value programatically in C# application running under .NETCoreApp framework? I'm particularly interested in its Sdks
subdirectory, as I need to provide it to a hosted instance of MSBuild when processing some .NET Core projects. Therefore, properties such as AppContext.BaseDirectory
are no use to me, because they point to the path of the current application.
I'll probably end up launching dotnet --info
and parsing its results, but I wonder whether a more elegant way exists. Thanks.
EDIT: Originaly there was mistakenly dotnet --version
instead of dotnet --info
.
You could run the "dotnet --info" command from your application and parse the output.
Quick and dirty:
class Program
{
static void Main(string[] args)
{
var basePath = GetDotNetCoreBasePath();
Console.WriteLine();
Console.ReadLine();
}
static String GetDotNetCoreBasePath()
{
Process process = new Process
{
StartInfo =
{
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
FileName = "dotnet",
Arguments = "--info"
}
};
process.Start();
process.WaitForExit();
if (process.HasExited)
{
string output = process.StandardOutput.ReadToEnd();
if (String.IsNullOrEmpty(output) == false)
{
var reg = new Regex("Base Path:(.+)");
var matches = reg.Match(output);
if (matches.Groups.Count >= 2)
return matches.Groups[1].Value.Trim();
}
}
throw new Exception("DotNet Core Base Path not found.");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With