6

dotnet 5 判断当前程序运行在 Windows 系统 Linux 系统的简单方法

 2 years ago
source link: https://lindexi.gitee.io/post/dotnet-5-%E5%88%A4%E6%96%AD%E5%BD%93%E5%89%8D%E7%A8%8B%E5%BA%8F%E8%BF%90%E8%A1%8C%E5%9C%A8-Windows-%E7%B3%BB%E7%BB%9F-Linux-%E7%B3%BB%E7%BB%9F%E7%9A%84%E7%AE%80%E5%8D%95%E6%96%B9%E6%B3%95.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

dotnet 5 判断当前程序运行在 Windows 系统 Linux 系统的简单方法

本文告诉大家使用 dotnet 5 提供的 System.OperatingSystem 类的方法进行快速且简单判断当前程序所运行在的系统

判断系统的简单代码示例:

            if (System.OperatingSystem.IsWindows())
            {
                if (System.OperatingSystem.IsWindowsVersionAtLeast(10, 0, 19043))
                {

                }
            }
            else if (System.OperatingSystem.IsLinux())
            {

            }
            else if (System.OperatingSystem.IsMacOS())
            {

            }
            else if (System.OperatingSystem.IsIOS())
            {

            }
            else if (System.OperatingSystem.IsAndroid())
            {

            }
            else if (System.OperatingSystem.IsBrowser())
            {

            }
            else if (System.OperatingSystem.IsTvOS())
            {

            }
            else if (System.OperatingSystem.IsFreeBSD())
            {

            }

可以很方便通过 IsXx 的方式判断当前是运行在哪个系统上。可以通过 IsXxVersionAtLeast 这一组方法判断是否当前运行的系统版本大于等于给定的版本

通过这些功能,可以很方便编写特定功能的代码

在 dotnet 5 之前,需要通过 RuntimeInformation.IsOSPlatform 方法进行判断系统,代码如下

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                
            }

更多请看 OperatingSystem Class (System) Microsoft Docs


本文会经常更新,请阅读原文: https://blog.lindexi.com/post/dotnet-5-%E5%88%A4%E6%96%AD%E5%BD%93%E5%89%8D%E7%A8%8B%E5%BA%8F%E8%BF%90%E8%A1%8C%E5%9C%A8-Windows-%E7%B3%BB%E7%BB%9F-Linux-%E7%B3%BB%E7%BB%9F%E7%9A%84%E7%AE%80%E5%8D%95%E6%96%B9%E6%B3%95.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

如果你想持续阅读我的最新博客,请点击 RSS 订阅,推荐使用RSS Stalker订阅博客,或者前往 CSDN 关注我的主页

本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接: https://blog.lindexi.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系

无盈利,不卖课,做纯粹的技术博客

以下是广告时间

推荐关注 Edi.Wang 的公众号
lindexi%2F201985113622445

欢迎进入 Eleven 老师组建的 .NET 社区
lindexi%2F20209121930471745.jpg

以上广告全是友情推广,无盈利


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK