System.Environment.OSVersion在.NET Core 5.0中有替代品吗?

3

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

我正在尝试确定用户使用的操作系统,以便在将文件保存到文件系统时,我知道是使用 '/' 还是 '\'。

那么我应该使用什么替代方法呢?


4
显然,这个功能已经被“故意忽略”(https://github.com/dotnet/corefx/issues/1017),以防止开发人员使用版本号来检查功能的可用性。你想要解决的根本问题是什么? 答案:显然,为了防止开发人员使用版本号来检查功能是否可用,该功能被故意省略。您想要解决的潜在问题是什么? - Heinzi
这个问题在 Github 上有讨论:https://github.com/dotnet/corefx/issues/1017。目前还没有这个功能。 - Pawel Maga
我正在尝试确定用户使用的操作系统,以便在他们将文件保存到文件系统时,我知道是使用“/”还是“\”。 - allencoded
1
你可以使用 Path.Combine(除非它也被省略了)。 - Theodoros Chatzigiannakis
2个回答

6

0

你可以把它取回:

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;
            }
        }

    }


}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接