【问题标题】:System.Environment.OSVersion .NET Core 5.0 Framework replacement?System.Environment.OSVersion .NET Core 5.0 框架替换?
【发布时间】:2016-02-21 05:37:25
【问题描述】:

System.Environment.OSVersion 似乎不是 .net core 5.0 (dnxcore50) 的一部分。

我正在尝试确定用户使用的是哪个操作系统,因此当他们将文件保存到我知道的文件系统时,我知道使用 '/' 或 '\'

我应该改用什么?

【问题讨论】:

  • 显然,这个功能已经deliberately left out 防止开发者使用版本号来检查功能可用性。您要解决的根本问题是什么?
  • github 上有关于这个问题的讨论:github.com/dotnet/corefx/issues/1017。目前此功能不可用
  • 我正在尝试确定用户使用的是哪个操作系统,因此当他们将文件保存到文件系统时,我知道使用'/'或'\'
  • 你可以使用Path.Combine(除非它也被排除在外)。

标签: c# .net-core


【解决方案1】:

不要自己进行检测。使用System.IO.Path.DirectorySeparatorChar。此外,如果您总是使用 Path 来操作路径,那么您应该获得所需的格式。这也适用于 URL。

【讨论】:

    【解决方案2】:

    你可以拿回来:

    using System.Runtime.InteropServices;
    
    namespace System
    {
    
    
        // [ComVisible(true)]
        // [Serializable]
        public enum PlatformID
        {
            Win32S = 0,
            Win32Windows = 1,
            Win32NT = 2,
            WinCE = 3,
            Unix = 4,
            // Since NET 3.5 SP1 or silverlight
            Xbox = 5,
            MacOSX = 6,
        }
    
    
        public class OperatingSystem
        {
            private Architecture m_arch;
            private bool m_isARM;
            private bool m_isX86;
            private bool m_isLinux;
            private bool m_isOSX;
            private bool m_isWindows;
            private PlatformID m_platform;
    
            public OperatingSystem()
            {
                m_arch = RuntimeInformation.ProcessArchitecture;
                m_isARM = (m_arch == Architecture.Arm || m_arch == Architecture.Arm64);
                m_isX86 = (m_arch == Architecture.X86 || m_arch == Architecture.X64);
    
                m_isLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
                m_isOSX = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
                m_isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
    
                if (m_isLinux || m_isOSX)
                    m_platform = PlatformID.Unix;
                else if (m_isWindows)
                {
                    m_platform = PlatformID.Win32NT;
                }
                else
                    throw new System.PlatformNotSupportedException("Operating system not supported.");
            }
    
            public PlatformID Platform
            {
                get
                {
                    return m_platform;
                }
            }
        }
    
    
    
        public class OldEnvironment
        {
    
            private static OperatingSystem m_OS;
    
            static OldEnvironment()
            {
                m_OS = new OperatingSystem();
            }
    
            public static OperatingSystem OSVersion
            {
                get
                {
                    return m_OS;
                }
            }
    
        }
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 2017-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-01
      • 2021-07-09
      • 1970-01-01
      • 2020-02-29
      • 1970-01-01
      相关资源
      最近更新 更多