C# preprocessor differentiate between operating systems

前端 未结 3 1401
甜味超标
甜味超标 2020-12-31 05:21

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

#if OS_WINDOWS
//windows methods
#elif O         


        
3条回答
  •  长发绾君心
    2020-12-31 06:24

    No. Sadly you can't. And it is even logical: if you compile for AnyCPU, then your program is executable on any platform.

    What you can do is create multiple project configurations, where you set the #define you want (in the Properties of the project, Build, Conditional compilation symbols).

    But perhaps this is a XY problem... Normally you don't need to do it, and you can live with a

    if (Environment.OSVersion.Platform == PlatformID.Win32NT)
    {
    
    }
    else if (Environment.OSVersion.Platform == PlatformID.MacOSX)
    {
    
    }
    else if (Environment.OSVersion.Platform == PlatformID.Unix)
    {
    
    }
    

提交回复
热议问题