Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 确定.NET核心应用程序中的运行时目标(OS)_C#_.net Core - Fatal编程技术网

C# 确定.NET核心应用程序中的运行时目标(OS)

C# 确定.NET核心应用程序中的运行时目标(OS),c#,.net-core,C#,.net Core,My.NET Core 3.0应用程序针对不同的操作系统发布,例如使用命令dotnet publish-r win10-x64或dotnet publish-r ubuntu.18.04-x64 在运行期间,在我的C代码中,我想找出构建应用程序的目标。我指的不仅仅是像Windows或Linux这样的通用操作系统(如要求的那样),而是确切的运行时目标,如ubuntu-18.04-x64 我已经发现,有一个文件.deps.json。它包含属性“runtimeTarget”:{“name:”.NETC

My.NET Core 3.0应用程序针对不同的操作系统发布,例如使用命令
dotnet publish-r win10-x64
dotnet publish-r ubuntu.18.04-x64

在运行期间,在我的C代码中,我想找出构建应用程序的目标。我指的不仅仅是像Windows或Linux这样的通用操作系统(如要求的那样),而是确切的运行时目标,如
ubuntu-18.04-x64


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

我将下面给出的代码用于.Net core版本2(以及过去的1.2)——

public static void PrintTargetRuntime()
{
var framework=Assembly
.GetEntryAssembly()?
.GetCustomAttribute()?
.框架名称;
var stats=新
{
OsPlatform=System.Runtime.InteropServices.RuntimeInformation.OSDescription,
OSArchitecture=System.Runtime.InteropServices.RuntimeInformation.OSArchitecture,
ProceArchitecture=System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture,
FrameworkDescription=System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription,
AspDotnetVersion=framework
};
Console.WriteLine(“框架版本为”+Framework);
控制台.WriteLine(“操作系统平台是:”+stats.OsPlatform);
WriteLine(“操作系统架构是:”+stats.OSArchitecture);
WriteLine(“框架描述为”+stats.FrameworkDescription);
Console.WriteLine(“ASPDotNetVersion是”+stats.ASPDotNetVersion);
if(stats.ProcesArchitecture==Architecture.Arm)
{
控制台。WriteLine(“ARM进程”);
}
else if(stats.ProcesArchitecture==Architecture.Arm64)
{
WriteLine(“ARM64进程”);
}
else if(stats.ProcesArchitecture==Architecture.X64)
{
控制台写入线(“X64进程”);
}
else if(stats.ProcesArchitecture==Architecture.X86)
{
WriteLine(“x86进程”);
}
}
我已经在Windows10和MacOS Mojave上测试过了。 这来自这里-

在我的windows计算机上,输出如下所示-

因为我没有找到其他方法,所以我正在使用
.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;
    }
}
使用Newtonsoft.Json.Linq;
使用制度;
使用System.IO;
/// 
///返回此应用程序运行的当前RID(运行时标识符)。
///看https://docs.microsoft.com/en-us/dotnet/core/rid-catalog 获取可能的值,例如“ubuntu.18.04-x64”。
///该值从应用程序文件夹中第一个找到的.deps.json文件读取,路径为
///“runtimeTarget”/“name”最后一个“/”后面的值。
///如果缺少文件或最后一个“/”后面的值,则未编译此应用程序文件夹
///对于特定的运行时,返回null。
/// 
公共静态字符串?GetRuntimeIdentifier(){
试一试{
//首先在应用程序文件夹中查找(可能仅限于).deps.json文件。
var dir=AppDomain.CurrentDomain.BaseDirectory;
var files=Directory.GetFiles(dir,“*.deps.json”);
如果(files.Length==0)
返回null;
//读取JSON内容
var json=JObject.Parse(File.ReadAllText(Path.Combine(dir,files[0]));
var name=json[“runtimeTarget”][“name”].ToString();
//斜杠后读
var slashPos=name.LastIndexOf('/');
如果(slashPos==-1)
返回null;
返回name.Substring(slashPos+1);
}
抓住{
//意外的文件格式或其他问题
返回null;
}
}

您特别需要整个运行时标识符?您将无法从任何其他成员处获得所需的信息?是的,我需要整个标识符。例如,
RuntimeInformation.OSDescription
=
Microsoft Windows 10.0.17763
RuntimeInformation.OSArchitecture
=
X64
,我看不到用这些值重建
win10-x64
运行时平台的可靠且经得起未来考验的方法。我只能在那里找到值
.NETCoreApp,Version=v3.0
,但我需要的是
win10-x64
ubuntu.18.04-x64
。检查stats.OSPlatform内容,它会打印操作系统版本关于更新上面回答中的图片。请参见我在问题下方的评论。
RuntimeInformation
对我没有帮助:我需要完整的标识符。例如,
RuntimeInformation.OSDescription
=
Microsoft Windows 10.0.17763
RuntimeInformation.OSArchitecture
=
X64
,我看不到任何关系从这些值重建
win10-x64
运行时平台是一种可行且经得起未来考验的方法。抱歉,我得到了您想要的东西,但有点晚了。您所说的是RID()但这与从RuntimeInformation类中提取的信息相同。事实上,根据文档,已发布的RID特定于您的项目文件,而不是您运行的二进制/进程。这非常有帮助,使我不必亲自问这个问题。对于未来的搜索者:“C#确定.NET运行时名称”
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;
    }
}