当前位置:K88软件开发文章中心编程语言.NET.NET02 → 文章内容

用C#获取CPU编号、硬盘编号等与系统有关的环境属性

减小字体 增大字体 作者:佚名  来源:翔宇亭IT乐园  发布时间:2019-1-3 0:48:10

/ 获取系统目录
    /// </summary>
    /// <returns></returns>
    public string GetSysDirectory()
    ...{
        StringBuilder sBuilder = new StringBuilder(CHAR_COUNT);
        GetSystemDirectory(sBuilder, CHAR_COUNT);
        return sBuilder.ToString();
    }

   /**//// <summary>
   /// 获取CPU信息
   /// </summary>
   /// <returns></returns>
    public CpuInfo GetCpuInfo()
    ...{
        CpuInfo cpuInfo = new CpuInfo();
        GetSystemInfo(ref cpuInfo);
        return cpuInfo;
    }

    /**//// <summary>
    /// 获取系统内存信息
    /// </summary>
    /// <returns></returns>
    public MemoryInfo GetMemoryInfo()
    ...{
        MemoryInfo memoryInfo = new MemoryInfo();
        GlobalMemoryStatus(ref memoryInfo);
        return memoryInfo;
    }

    /**//// <summary>
    /// 获取系统时间信息
    /// </summary>
    /// <returns></returns>
    public SystemTimeInfo GetSystemTimeInfo()
    ...{
        SystemTimeInfo systemTimeInfo = new SystemTimeInfo();
        GetSystemTime(ref systemTimeInfo);
        return systemTimeInfo;
    }

    /**//// <summary>
    /// 获取系统名称
    /// </summary>
    /// <returns></returns>
    public string GetOperationSystemInName()
    ...{
        OperatingSystem os = System.Environment.OSVersion;
        string osName = "UNKNOWN";
        switch (os.Platform)
        ...{
            case PlatformID.Win32Windows:
                switch (os.Version.Minor)
                ...{
                    case 0: osName = "Windows 95"; break;
                    case 10: osName = "Windows 98"; break;
                    case 90: osName = "Windows ME"; break;
                }
                break;
            case PlatformID.Win32NT:
                switch (os.Version.Major)
                ...{
                    case 3: osName = "Windws NT 3.51"; break;
                    case 4: osName = "Windows NT 4"; break;
                    case 5: if (os.Version.Minor == 0)
                        ...{
                            osName = "Windows 2000";
                        }
                        else if (os.Version.Minor == 1)
                        ...{
                            osName = "Windows XP";
                        }
                        else if (os.Version.Minor == 2)
                        ...{
                            osName = "Windows Server 2003";
                        }
                        break;
                    case 6: osName = "Longhorn"; break;
                }
                break;
        }
        return String.Format("{0},{1}", osName, os.Version.ToString());
    }
}
以下是调用实例,为了简单,我在一个aspx页面中输出,不过这个程序可以在WinForm中调用:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Runtime.InteropServices;

public partial class Index : System.Web.UI.Page
...{
    protected void Page_Load(object sender, EventArgs e)
    ...{
        if (!Page.IsPostBack)
        ...{
            SystemInfo systemInfo = new SystemInfo();
             Response.Write("操作系统:" + systemInfo.GetOperationSystemInName() + "<br>");
            Response.Write("CPU编号:"+systemInfo.GetCpuId() + "<br>");
            Response.Write("硬盘编号:"+systemInfo.GetMainHardDiskId() + "<br>");
            Response.Write("Windows目录所在位置:" + systemInfo.GetSysDirectory() + "<br>");
            Response.Write("系统目录所在位置:" + systemInfo.GetWinDirectory() + "<br>");
            MemoryInfo memoryInfo = systemInfo.GetMemoryInfo();
            CpuInfo cpuInfo = systemInfo.GetCpuInfo();
            Response.Write("dwActiveProcessorMask" + cpuInfo.dwActiveProcessorMask + "<br>");
            Response.Write("dwAllocationGranularity" + cpuInfo.dwAllocationGranularity + "<br>");
            Response.Write("CPU个数:" + cpuInfo.dwNumberOfProcessors + "<br>");
            Response.Write("OEM ID:" + cpuInfo.dwOemId + "<br>");
            Response.Write("页面大小" + cpuInfo.dwPageSize + "<br>");
            Response.Write("CPU等级" + cpuInfo.dwProcessorLevel + "<br>");
            Response.Write("dwProcessorRevision" + cpuInfo.dwProcessorRevision + "<br>");
            Response.Write("CPU类型" + cpuInfo.dwProcessorType + "<br>");
            Response.Write("lpMaximumApplicationAddress" + cpuInfo.lpMaximumApplicationAddress + "<br>");
            Response.Write("lpMinimumApplicationAddress" + cpuInfo.lpMinimumApplicationAddress + "<br>");
            Response.Write("CPU类型:" + cpuInfo.dwProcessorType + "<br>");
            Response.Write("可用交换文件大小:" + memoryInfo.dwAvailPageFile + "<br>");
            Response.Write("可用物理内存大小:" + memoryInfo.dwAvailPhys + "<br>");
            Response.Write("可用虚拟内存大小" + memoryInfo.dwAvailVirtual + "<br>");
            Response.Write("操作系统位数:" + memoryInfo.dwLength + "<br>");
            Response.Write("已经使用内存大小:" + memoryInfo.dwMemoryLoad + "<br>");
            Response.Write("交换文件总大小:" + memoryInfo.dwTotalPageFile + "<br>");
            Response.Write("总物理内存大小:" + memoryInfo.dwTotalPhys + "<br>");
            Response.Write("总虚拟内存大小:" + memoryInfo.dwTotalVirtual + "<br>");
        }
    }
}
说明:前台aspx页面没有任何控件。


上一页  [1] [2] 


用C#获取CPU编号、硬盘编号等与系统有关的环境属性