C#预处理程序区分操作系统 [英] C# preprocessor differentiate between operating systems

查看:82
本文介绍了C#预处理程序区分操作系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用预处理器 C#中区分操作系统?像这样:

Is it possible to differentiate between operating systems in C# using preprocessor? like :

#if OS_WINDOWS
//windows methods
#elif OS_MAC
//mac  methods
#elif OS_LINUX
//linux methods
#endif


推荐答案

您所要求的是可能的,但需要一些工作。

What you are asking for is possible but needs a bit of work.


  1. csproj

<PropertyGroup Condition=" '$(OS)' == 'Windows_NT' ">
  <DefineConstants>_WINDOWS</DefineConstants>
</PropertyGroup>


  • 在代码中使用它

  • Use that in your code

    #if _WINDOWS
      // your windows stuff
    #else
      // your *nix stuff
    #endif
    


  • 当您拥有依赖于操作系统(例如本机库名称)

    I find this technique useful when you have constants that are dependent on the OS (for example native library names)

    这篇关于C#预处理程序区分操作系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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