加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – csproj根据操作系统复制文件

发布时间:2020-12-15 08:36:08 所属栏目:百科 来源:网络整理
导读:我正在使用.NET Core构建跨平台类库.根据为使用.csproj文件构建C#.NET Core项目的操作系统,我需要将本机库复制到项目的输出目录.例如,对于OS X我想复制.dylib文件,对于 Windows我要复制.DLL文件,对于 Linux我要复制.so文件. 如何使用.csproj ItemGroup中的Co
我正在使用.NET Core构建跨平台类库.根据为使用.csproj文件构建C#.NET Core项目的操作系统,我需要将本机库复制到项目的输出目录.例如,对于OS X我想复制.dylib文件,对于 Windows我要复制.DLL文件,对于 Linux我要复制.so文件.

如何使用.csproj ItemGroup中的Condition子句执行此操作?

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

$(平台)似乎不起作用.我可以使用不同的变量吗?

解决方法

用于区分Windows和Windows Mac / Linux你可以使用$(os)属性:这为你提供了适用于Windows的Windows_NT和适用于Mac / Linux的UNIX.

为了区分Mac和& Linux,至少在最新版本的MSBuild上,你可以使用RuntimeInformation.IsOSPlatform.

所以,结合起来,你的csproj可以有这样的东西:

<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的所有最新版本.

在旧版本中,您需要诉诸恶意技巧来区分Mac& Linux的:

对于Linux – > Condition =“’$(OS)’==’Unix’和!$([System.IO.File] :: Exists(‘/usr/lib / libc.dylib’))”

对于Mac – > Condition =“’$(OS)’==’Unix’和$([System.IO.File] :: Exists(‘/usr/lib / libc.dylib’))”

资料来源:

> https://github.com/Microsoft/msbuild/issues/539
> https://github.com/Microsoft/msbuild/issues/2468

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读