简体   繁体   English

如何获取系统范围的.NET Core运行时环境的基本路径?

[英]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.

相关问题 如何在 .NET Core 中设置全局环境变量(用户范围或系统范围) - How to set a global environment variable in .NET Core (user-wide or system-wide) 是.net系统范围内的垃圾收集器还是应用程序范围内的垃圾收集器? - Is the garbage collector in .net system-wide or application-wide? .NET系统范围内的EventWaitHandle名称允许的字符 - .NET system-wide EventWaitHandle name allowed characters 应用程序如何与系统范围的文本选择挂钩? - How can an app hook into text selection system-wide? 与“系统级”媒体播放器互动 - Interact with “system-wide” media player 取消处理(取消注册)系统范围的“HotKey” - Unhandling (unregistering) a system-wide 'HotKey' 如何在生产和开发环境中始终如一地获取ASP.NET 5 DNX项目的应用程序基础路径? - How to consistently get application base path for ASP.NET 5 DNX project on both production and development environment? 如何在C#中通过Windows资源管理器有效监视系统范围的“文件名更改事件” - How to monitor system-wide “file name change event” by windows explorer effectively in c# 系统范围的热键 - 如何以编程方式检测全局键盘快捷键? - system-wide hot keys — how to detect global keyboard shortcuts programmatically? 如何在 ASP.net 内核中在运行时更新 HttpClient 基地址 - How to update HttpClient base address at runtime in ASP.net core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM