[英]How to get the base path of system-wide .NET Core runtime environment?
When I run dotnet --info
, among other information I receive also: 当我运行
dotnet --info
,我收到的其他信息也包括:
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? 有没有办法在.NETCoreApp框架下运行的C#应用程序中以编程方式获取此值? 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. 我对它的
Sdks
子目录特别感兴趣,因为我需要在处理一些.NET Core项目时将它提供给MSBuild的托管实例。 Therefore, properties such as AppContext.BaseDirectory
are no use to me, because they point to the path of the current application. 因此,
AppContext.BaseDirectory
等属性对我没用,因为它们指向当前应用程序的路径。
I'll probably end up launching dotnet --info
and parsing its results, but I wonder whether a more elegant way exists. 我可能最终会启动
dotnet --info
并解析其结果,但我想知道是否存在更优雅的方式。 Thanks. 谢谢。
EDIT: Originaly there was mistakenly dotnet --version
instead of dotnet --info
. 编辑:原来有错误的
dotnet --version
而不是dotnet --info
。
You could run the "dotnet --info" command from your application and parse the output. 您可以从应用程序运行“dotnet --info”命令并解析输出。
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.");
}
}
After installing this package https://www.nuget.org/packages/Microsoft.DotNet.PlatformAbstractions/ 安装此软件包后https://www.nuget.org/packages/Microsoft.DotNet.PlatformAbstractions/
ApplicationEnvironment.ApplicationBasePath
will give you what you're looking for. ApplicationEnvironment.ApplicationBasePath
将为您提供所需的内容。 I found this by looking at the dotnet
source code... 我通过查看
dotnet
源代码找到了这个...
https://github.com/dotnet/cli/blob/f62ca5e235e253c033c628a39852d5923d53688a/src/dotnet/Program.cs#L251 https://github.com/dotnet/cli/blob/f62ca5e235e253c033c628a39852d5923d53688a/src/dotnet/Program.cs#L251
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.