csproj复制文件取决于操作系统

编程入门 行业动态 更新时间:2024-10-28 06:21:31
本文介绍了csproj复制文件取决于操作系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用.NET Core构建跨平台类库。根据为使用.csproj文件而构建的C#.NET Core项目的操作系统,我需要将本机库复制到项目的输出目录。例如,对于OS XI,要复制.dylib文件,对于Windows,我要复制.DLL文件,对于Linux,我要复制.so文件。

I am using .NET Core to build a cross platform class library. Depending on the operating system that the C# .NET Core project is built for using a .csproj file, I need to copy a native library to the project's output directory. E.g., for OS X I want to copy a .dylib file, for Windows I want to copy a .DLL file, for Linux I want to copy a .so file.

如何在.csproj ItemGroup中使用Condition子句来做到这一点?

How can I do this with a Condition clause in a .csproj ItemGroup?

<ItemGroup> <Content Include="libNative.dylib" Condition=" '$(Configuration)|$(Platform)' == 'Debug|OSX' "> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> </ItemGroup>

$(Platform)工作。我可以使用其他变量吗?

$(Platform) does not seem to work. Is there a different variable I can use?

推荐答案

用于区分Windows和Windows。在Mac / Linux中,您可以使用 $(os)属性:这将为Windows提供 Windows_NT 和用于Mac / Linux的UNIX 。

For differentiating between Windows & Mac/Linux you can use the $(os) property: this gives you Windows_NT for Windows and UNIX for Mac/Linux.

用于区分Mac和Linux,至少在最新版本的MSBuild上,您可以使用 RuntimeInformation.IsOSPlatform 。

For differentiating between Mac & Linux, at least on recent versions of MSBuild, you can use RuntimeInformation.IsOSPlatform.

因此, csproj可以具有以下内容:

So, combined, your csproj can have something like this:

<ItemGroup> <Content Include="libNative.dll" Condition=" '$(OS)' == 'Windows_NT' "> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> <Content Include="libNative.so" Condition=" '$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Linux)))' "> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> <Content Include="libNative.dylib" Condition=" '$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::OSX)))' "> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> </ItemGroup>

据我所知,这应该适用于所有最新版本的.Net Core,Mono和 Framework。

To the best of my knowledge, this should work in all recent versions of .Net Core, Mono and .Net Framework.

在较旧的版本中,您需要采取恶作剧的手段来区分Mac和Linux:

In older versions, you'd need to resort to evil trickery for differentiating between Mac & Linux:

对于Linux-> Condition ='$(OS)'=='Unix'和!$([System.IO。 File] :: Exists('/ usr / lib / libc.dylib'))

For Linux -> Condition=" '$(OS)' == 'Unix' and ! $([System.IO.File]::Exists('/usr/lib/libc.dylib')) "

对于Mac-> Condition ='$(OS)'=='Unix'和$([System.IO.File] :: Exists('/ usr / lib / libc.dylib'))

来源:

  • github/Microsoft/msbuild/issues/539
  • github/Microsoft/msbuild/issues/2468
  • github/Microsoft/msbuild/issues/539
  • github/Microsoft/msbuild/issues/2468

更多推荐

csproj复制文件取决于操作系统

本文发布于:2023-11-12 19:31:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582329.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:操作系统   文件   csproj

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!