管理

C#判断操作系统位数 - 开源研究系列文章

Posted on 2024-03-18 19:44  lzhdim  阅读(10914)  评论(0编辑  收藏  举报

       今天将开发的那个校时管理器应用程序复制到公司的电脑上进行使用,结果运行的时候报错了,想着应该是操作系统位数不支持导致的,于是写了此文进行判断。(对于有源码的读者请自己编译程序为32位的进行使用)

       该代码非常简单,就是判断一下句柄的长度:

  1、源码;

/***

    检测操作系统位数的操作类

    Austin Liu 刘恒辉
    Project Manager and Software Designer

    E-Mail: lzhdim@163.com
    Blog:   http://lzhdim.cnblogs.com
    Date:   2024-01-15 15:18:00

    使用方法:
        if (CheckSystemBitHelper.SystemBit() == 32)
        {
            MessageBox.Show("该应用不支持32位操作系统!", Application.ProductName + "-提示", MessageBoxButtons.OK);
            return;
        }

***/

namespace Lzhdim.Helper
{
    using System;

    /// <summary>
    /// 操作系统Bit位数获取操作类
    /// </summary>
    public static class CheckSystemBitHelper
    {
        /// <summary>
        /// 检查操作系统位数
        /// </summary>
        /// <returns>64 64位;32 32位;0 未知;</returns>
        public static int SystemBit()
        {
            //通过类型长度进行判断
            switch (IntPtr.Size)
            {
                case 8:
                    return 64;

                case 4:
                    return 32;

                default:
                    return 0;
            }
        }
    }
}

  2、使用:

/***

    应用程序入口类库

    Austin Liu 刘恒辉
    Project Manager and Software Designer

    E-Mail: lzhdim@163.com
    Blog:   http://lzhdim.cnblogs.com
    Date:   2024-01-15 15:18:00

    说明:
        1、做应用程序唯一运行判断;
        2、对应用程序的消息进行判断处理;
        3、启动应用程序操作;

***/

namespace Lzhdim.LzhdimSoft
{
    using System;
    using System.Windows.Forms;

    using Lzhdim.WindowMessage;
    using Lzhdim.Helper;

    using Lzhdim.LzhdimSoft.UI;

    /// <summary>
    /// 应用程序类
    /// </summary>
    internal static class Program
    {
        /// <summary>
        /// 应用程序的主入口点
        /// </summary>
        [STAThread]
        private static void Main(string[] args)
        {
            if (CheckSystemBitHelper.SystemBit() == 32)
            {
                MessageBox.Show("该应用不支持32位操作系统!", Application.ProductName + "-提示", MessageBoxButtons.OK);
                return;
            }

            if (AppInstance.IsUniqueRunning())
            {
                //如果应用程序已经启动,给其传应用消息进行处理
                WndMsgHelper.SendMessage2Thread(AppInstance.GetAppProcess().Threads[0].Id, (uint)WindowMessage.WM_ShowMainForm);
                return;
            }

            App.Start(args);
        }
    }
}

 

  上面介绍了检测操作系统位数的代码,不过现在Windows 11已经是完全64位的操作系统了,所以其它的应用直接编译为64位就行,至于32位的估计还是老电脑才存在这个问题了。

Copyright © 2000-2022 Lzhdim Technology Software All Rights Reserved