有谁知道如何在.NET Core中获取有关当前环境和平台的信息?目前Core库中没有Environment类.
dotnet publish -r win10-x64
我的 .NET Core 3.0 应用程序是使用命令或dotnet 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", ...
,但也许有更好的方法?
我的.NET Core库需要从注册表中读取一些信息(如果可用),或者保留默认值(如果没有)。我想知道这样做的最佳做法。
我认为我可以将注册表初始化/使用代码块包装在try / catch中,也可以检查当前平台是否为Windows,但我不认为这些是最佳做法(最好避免出现异常,并且不能保证任何Windows-基于平台的将具有注册表等)。
现在,我将依靠
bool hasRegistry = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
Run Code Online (Sandbox Code Playgroud)
但想知道是否有更可靠/通用的解决方案。
我有一个适用于Linux和Windows的应用程序.我需要知道应用程序在哪里使用差异代码.谢谢
asp.net-core-mvc .net-core asp.net-core asp.net-core-1.0 .net-core-rc2
我正在尝试使用DinkToPdf库从 HTML SQL 服务器数据库生成 PDF 。
在我添加的启动文件中:
var context = new CustomAssemblyLoadContext();
context.LoadUnmanagedLibrary(Path.Combine(Directory.GetCurrentDirectory(), "libwkhtmltox.dll"));
Run Code Online (Sandbox Code Playgroud)
该行在启动网络应用程序时给了我这个错误:
DllNotFoundException:无法加载 DLL 'C:\Program Files\IIS Express\libwkhtmltox.dll' 或其依赖项之一:找不到指定的模块。(来自 HRESULT 的异常:0x8007007E)
System.Runtime.Loader.AssemblyLoadContext.InternalLoadUnmanagedDllFromPath(string unmanagedDllPath)
DllNotFoundException:无法加载 DLL 'C:\Program Files\IIS Express\libwkhtmltox.dll' 或其依赖项之一:找不到指定的模块。(来自 HRESULT 的异常:0x8007007E)
我正在编写一个我希望在 Windows 和 Linux 上使用的类。
此类中的方法之一是访问Windows 注册表。
我希望实现的是在使用 Linux 计算机时以某种方式禁用此特定方法。
首先,我做了一些研究,看看.Net Core是否有一些东西可以让我检查正在使用哪个操作系统,我发现了这个并且确实可以工作。
当我在访问方法时将其实现到代码中时,我希望禁用访问 Windows 注册表的方法,但是我最接近的方法是使用 switch 语句,如下所示
switch (OS)
{
case OSX:
return;
case LINUX:
return
}
Run Code Online (Sandbox Code Playgroud)
如果return
操作系统不受支持,这可行,但是我随后认为禁用它一起访问会比为不支持该特定方法的操作系统抛出错误要好得多,
然后我继续查看预处理器指令认为,如果我能够检测并禁用取决于框架等的部分代码,也许我可以使用类似的东西来根据操作系统禁用部分代码,这样即使在尝试访问时也永远不会调用它们我从那里继续的方法
是看看我是否可以使用禁用部分代码preprocessor directives
。
我找到了这个。
我知道它是针对C++的,但它似乎是我能找到的最接近我想要实现的目标的在.Net Core
一个完美的世界中,它看起来像这样
/// <summary>
/// Get the file mime type
/// </summary>
/// <param name="filePathLocation">file path location</param>
/// <returns></returns>
`#if WINDOWS`
public static string GetMimeType(this string filePathLocation)
{
if (filePathLocation.IsValidFilePath())
{ …
Run Code Online (Sandbox Code Playgroud) 我使用 TopShelf 创建了一个 .net 核心控制台应用程序。但是在使用 docker (alpine-linux) 运行应用程序时出现错误。
Configuration Result:
[Success] Name MyApp
[Success] DisplayName MyApp
[Success] Description My Application
[Success] ServiceName MyApp
Topshelf v4.1.0.177, .NET Framework v4.0.30319.42000
Topshelf.Runtime.Windows.WindowsHostEnvironment Error: 0 : Unable to get parent process (ignored), System.DllNotFoundException: Unable to load shared library 'kernel32.dll' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: Error loading shared library libkernel32.dll: No such file or directory
at Topshelf.Runtime.Windows.Kernel32.CreateToolhelp32Snapshot(UInt32 dwFlags, UInt32 th32ProcessID)
at Topshelf.Runtime.Windows.WindowsHostEnvironment.GetParent(Process …
Run Code Online (Sandbox Code Playgroud) containers topshelf docker .net-core .net-core-configuration
.net-core ×7
c# ×5
.net ×1
asp.net-core ×1
containers ×1
dinktopdf ×1
docker ×1
html-to-pdf ×1
registry ×1
topshelf ×1
wkhtmltopdf ×1