在.NET Core应用中确定运行时目标(OS)

和我

我的.NET Core 3.0应用程序是使用命令dotnet publish -r win10-x64dotnet publish -r ubuntu.18.04-x64例如为不同的操作系统发布的

在运行时,我想用我的C#代码找出构建应用程序的目标。我的意思不是仅仅像Windows或Linux这样的通用操作系统(如此处所述),而是确切的运行时目标,如ubuntu-18.04-x64

我已经发现,有一个文件<AssemblyName>.deps.json它包含属性"runtimeTarget": { "name": ".NETCoreApp,Version=v3.0/ubuntu.18.04-x64", ...,但是也许有更好的方法?

和我

由于找不到其他方法,因此我正在使用.deps.json文件中找到的值这是我的代码:

using Newtonsoft.Json.Linq;
using System;
using System.IO;

/// <summary>
/// Returns the current RID (Runtime IDentifier) where this applications runs.
/// See https://docs.microsoft.com/en-us/dotnet/core/rid-catalog for possible values, e.g. "ubuntu.18.04-x64".
/// The value is read from the first found .deps.json file in the application folder, at the path
/// "runtimeTarget"/"name" the value behind the last "/".
/// When the file or the value behind the last "/" is missing, this application folder was not compiled
/// for a specific runtime, and null is returned.
/// </summary>
public static string? GetRuntimeIdentifier() {
    try {
        // Find first (and probably only) .deps.json file in the application's folder.
        var dir = AppDomain.CurrentDomain.BaseDirectory;
        var files = Directory.GetFiles(dir, "*.deps.json");
        if (files.Length == 0)
            return null;
        // Read JSON content
        var json = JObject.Parse(File.ReadAllText(Path.Combine(dir, files[0])));
        var name = json["runtimeTarget"]["name"].ToString();
        // Read RID after slash
        var slashPos = name.LastIndexOf('/');
        if (slashPos == -1)
            return null;
        return name.Substring(slashPos + 1);
    }
    catch {
        // Unexpected file format or other problem
        return null;
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何为自包含的跨平台 .NET Core 应用程序指定目标运行时?

来自分类Dev

Azure 应用服务 - 更新 .NET Core 运行时

来自分类Dev

Azure中.NET Core 3.0的运行时堆栈

来自分类Dev

无法发布.NET Core应用程序(不受支持的运行时)

来自分类Dev

如何获取独立部署 (SCD) 应用程序的 .NET Core 运行时版本?

来自分类Dev

在 ASP.NET Core Angular 应用程序运行时更改主题的热门话题

来自分类Dev

无法通过自定义运行时在Docker中运行.NET Core应用的App Engine连接到Google Cloud SQL中的Postgres

来自分类Dev

.Net Core 1.0中的运行时编译和运行代码

来自分类Dev

.Net Core 1.0中的运行时编译和运行代码

来自分类Dev

asp.net core 1.如何将目标运行时更改为x86

来自分类Dev

NET Framework 4.7.2应用程序可以在运行时加载NET Core 5.0库DLL吗?

来自分类Dev

从.NET应用程序运行时,SSIS包失败,仅在日志中显示OnPreValidate

来自分类常见问题

在ASP.NET Core MVC中在运行时动态绑定模型

来自分类Dev

NET Core库中如何使用Windows运行时类?

来自分类Dev

在ASP.NET Core MVC中在运行时动态绑定模型

来自分类Dev

在ASP.NET Core中在运行时扩展Autofac IoC容器配置

来自分类Dev

是否可以在.NET Core库中包含运行时指令?

来自分类Dev

如何在运行时更改 asp.net core 中的启动数据?

来自分类Dev

Linux 上 .NET 核心应用程序的运行时

来自分类Dev

在Linux上安装特定版本的.NET Core运行时

来自分类Dev

.NET Core 1.1 和运行时本地化

来自分类Dev

无法在3.1 docker容器中运行dotnet转储-“进程1未运行兼容的.NET Core运行时”

来自分类Dev

在Bamboo中运行时,不同的.NET构建结果

来自分类Dev

在SQL运行时捕获.NET中的SQL PRINT输出

来自分类Dev

自包含 .Net Core 库缺少 .NET Core 运行时文件

来自分类Dev

如何配置Kestrel以使用随机动态端口并在运行时使用ASP.NET Core 3.1确定端口?

来自分类Dev

ASP.NEt应用程序中HttpRequest.InputStream属性的运行时类型是什么

来自分类Dev

VB.NET在运行时确定项目的导入名称空间

来自分类Dev

Razor页面在运行时无法在ASP.NET Core RC2中看到引用的类库

Related 相关文章

  1. 1

    如何为自包含的跨平台 .NET Core 应用程序指定目标运行时?

  2. 2

    Azure 应用服务 - 更新 .NET Core 运行时

  3. 3

    Azure中.NET Core 3.0的运行时堆栈

  4. 4

    无法发布.NET Core应用程序(不受支持的运行时)

  5. 5

    如何获取独立部署 (SCD) 应用程序的 .NET Core 运行时版本?

  6. 6

    在 ASP.NET Core Angular 应用程序运行时更改主题的热门话题

  7. 7

    无法通过自定义运行时在Docker中运行.NET Core应用的App Engine连接到Google Cloud SQL中的Postgres

  8. 8

    .Net Core 1.0中的运行时编译和运行代码

  9. 9

    .Net Core 1.0中的运行时编译和运行代码

  10. 10

    asp.net core 1.如何将目标运行时更改为x86

  11. 11

    NET Framework 4.7.2应用程序可以在运行时加载NET Core 5.0库DLL吗?

  12. 12

    从.NET应用程序运行时,SSIS包失败,仅在日志中显示OnPreValidate

  13. 13

    在ASP.NET Core MVC中在运行时动态绑定模型

  14. 14

    NET Core库中如何使用Windows运行时类?

  15. 15

    在ASP.NET Core MVC中在运行时动态绑定模型

  16. 16

    在ASP.NET Core中在运行时扩展Autofac IoC容器配置

  17. 17

    是否可以在.NET Core库中包含运行时指令?

  18. 18

    如何在运行时更改 asp.net core 中的启动数据?

  19. 19

    Linux 上 .NET 核心应用程序的运行时

  20. 20

    在Linux上安装特定版本的.NET Core运行时

  21. 21

    .NET Core 1.1 和运行时本地化

  22. 22

    无法在3.1 docker容器中运行dotnet转储-“进程1未运行兼容的.NET Core运行时”

  23. 23

    在Bamboo中运行时,不同的.NET构建结果

  24. 24

    在SQL运行时捕获.NET中的SQL PRINT输出

  25. 25

    自包含 .Net Core 库缺少 .NET Core 运行时文件

  26. 26

    如何配置Kestrel以使用随机动态端口并在运行时使用ASP.NET Core 3.1确定端口?

  27. 27

    ASP.NEt应用程序中HttpRequest.InputStream属性的运行时类型是什么

  28. 28

    VB.NET在运行时确定项目的导入名称空间

  29. 29

    Razor页面在运行时无法在ASP.NET Core RC2中看到引用的类库

热门标签

归档