csproj copy files depending on operating system

后端 未结 2 412
深忆病人
深忆病人 2020-12-30 05:45

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 co

2条回答
  •  庸人自扰
    2020-12-30 05:49

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

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

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

    
        
          PreserveNewest
        
        
          PreserveNewest
        
        
          PreserveNewest
        
    
    

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

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

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

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

    Sources:

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

提交回复
热议问题