导航

C# 判断操作系统类型

Posted on 2009-02-03 11:01  Heclei  阅读(1585)  评论(0编辑  收藏  举报

        /// <summary>
        /// Gets a value indicating if the process is running in 64 bit environment.
        /// </summary>
        public static unsafe bool IsRunningOn64Bit
        {
            get { return (sizeof(IntPtr) == sizeof(long)); }
        }

        /// <summary>
        /// Gets a value indicating if the operating system is a Windows 2000 or a newer one.
        /// </summary>
        public static bool IsWindows2000OrNewer
        {
            get { return (Environment.OSVersion.Platform == PlatformID.Win32NT) && (Environment.OSVersion.Version.Major >= 5); }
        }

        /// <summary>
        /// Gets a value indicating if the operating system is a Windows XP or a newer one.
        /// </summary>
        public static bool IsWindowsXpOrNewer
        {
            get
            {
                return
                    (Environment.OSVersion.Platform == PlatformID.Win32NT) &&
                    (
                        (Environment.OSVersion.Version.Major >= 6) ||
                        (
                            (Environment.OSVersion.Version.Major == 5) &&
                            (Environment.OSVersion.Version.Minor >= 1)
                        )
                    );
            }
        }

        /// <summary>
        /// Gets a value indicating if the operating system is a Windows Vista or a newer one.
        /// </summary>
        public static bool IsWindowsVistaOrNewer
        {
            get { return (Environment.OSVersion.Platform == PlatformID.Win32NT) && (Environment.OSVersion.Version.Major >= 6); }
        }

 

 

编译项目:“可编译不安全代码”属性设置为true

方法如下:项目属性对话框->配置属性->生成->允许不安全代码块 设为"true\"