csproj 复制文件取决于操作系统 [英] csproj copy files depending on operating system

查看:24
本文介绍了csproj 复制文件取决于操作系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 .NET Core 构建跨平台类库.根据为使用 .csproj 文件构建 C# .NET Core 项目的操作系统,我需要将本机库复制到项目的输出目录.例如,对于 OS X,我想复制一个 .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 和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 和 .Net 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' and ! $([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' and $([System.IO.File]::Exists('/usr/lib/libc.dylib'))"

来源:

这篇关于csproj 复制文件取决于操作系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆