zoukankan      html  css  js  c++  java
  • C#-获取磁盘,cpu,内存信息

    获取磁盘信息
    
    zongdaxiao = GetHardDiskSpace("C") * 1.0 / 1024;
    user = GetHardDiskFreeSpace("C") * 1.0 / 1024;
    
     ///   
    /// 获取指定驱动器的空间总大小(单位为B) 
    ///   
    ///  只需输入代表驱动器的字母即可 (大写) 
    ///    
    public long GetHardDiskSpace(string str_HardDiskName)
    {
          long totalSize = new long();
          str_HardDiskName = str_HardDiskName + ":\";
          System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
          foreach (System.IO.DriveInfo drive in drives)
          {
                if (drive.Name == str_HardDiskName)
                {
                     totalSize = drive.TotalSize / (1024 * 1024);
                }
          }
                return totalSize;
    }
    
    ///   
    /// 获取指定驱动器的剩余空间总大小(单位为B) 
    ///   
    ///  只需输入代表驱动器的字母即可  
    ///    
    public long GetHardDiskFreeSpace(string str_HardDiskName)
    {
         long freeSpace = new long();
         str_HardDiskName = str_HardDiskName + ":\";
         System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
         foreach (System.IO.DriveInfo drive in drives)
         {
             if (drive.Name == str_HardDiskName)
             {
                    freeSpace = drive.TotalFreeSpace / (1024 * 1024);
             }
        }
        return freeSpace;
    }
    
    *****************************************************
    获取总内存(运行)
    zongneicun = GetMemoryStatus();
    
     [StructLayout(LayoutKind.Sequential)]
     public struct MEMORY_INFO
     {
             public uint dwLength;
             public uint dwMemoryLoad;
             public uint dwTotalPhys;
             public uint dwAvailPhys;
             public uint dwTotalPageFile;
             public uint dwAvailPageFile;
             public uint dwTotalVirtual;
             public uint dwAvailVirtual;
     }
     [DllImport("kernel32")]
     public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);
     MEMORY_INFO MemInfo = new MEMORY_INFO();
    
     private long GetMemoryStatus()
     {
         GlobalMemoryStatus(ref MemInfo);
         long totalMb = Convert.ToInt64(MemInfo.dwTotalPhys.ToString()) / 1024 / 1024;
         //long avaliableMb = Convert.ToInt64(MemInfo.dwAvailPhys.ToString()) / 1024 / 1024;
          return totalMb; 
          //MessageBox.Show("物理内存共有" + totalMb.ToString () + " MB" + "可使用的物理内存有" + avaliableMb.ToString () + " MB");
          //Console.WriteLine("物理内存共有" + totalMb + " MB");
          //Console.WriteLine("可使用的物理内存有" + avaliableMb + " MB");
    }
    
    
    ******************************************************
    
    //获得pro使用内存
    public int pnc()
    {
          Process[] pro = Process.GetProcesses();
          double total = 0;
          Process temp;
          int i;
          for (i = 0; i < pro.Length; i++)
          {
                temp = pro[i];
                total = temp.PrivateMemorySize + total;
           }
           return (int)(total / 1024 / 1024);
    }
    *******************************************************
    
    获取cpu使用率
    PerformanceCounter PC = new PerformanceCounter("Processor", "% Processor Time", "_Total");
    label1.Text = "Cpu: " + Convert.ToInt32(PC.NextValue()) + "%";
    
    *************************************************************
    获取系统运行时间
    label4.Text = "System Runtime: " + (Environment.TickCount / 60000).ToString();
    
    

  • 相关阅读:
    使用comet架构实现了一个基于网页的视频监控prototype!!!!哇哈哈庆祝一下
    Pixysoft.Framework.Noebe.Datamining 数据挖掘开发实录
    论创业成功!让大家的青春充满着无限美好的回忆
    新年第一篇 数据库备份恢复系统上线的挫折
    .Net FrameWork 4.0中使用EF向数据库插入数据报datatime2类型错误的解决办法
    RoRoWoBlog 开源博客系统介绍
    第一次偶然出现的“System.Data.Entity.dll”类型的异常
    序列化类型 System.Data.Entity.DynamicProxies 的对象时检测到循环引用
    我也来说说Entity Frame Work 4中的数据库优先和代码优先两种方式(2)
    Asp.net MVC 2 + Castle + NHibernate 项目实战(1)
  • 原文地址:https://www.cnblogs.com/csnd/p/12062158.html
Copyright © 2011-2022 走看看