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();
    
    

  • 相关阅读:
    SQL Server, Timeout expired.all pooled connections were in use and max pool size was reached
    javascript 事件调用顺序
    Best Practices for Speeding Up Your Web Site
    C语言程序设计 使用VC6绿色版
    破解SQL Prompt 3.9的几步操作
    Master page Path (MasterPage 路径)
    几个小型数据库的比较
    CSS+DIV 完美实现垂直居中的方法
    由Response.Redirect引发的"Thread was being aborted. "异常的处理方法
    Adsutil.vbs 在脚本攻击中的妙用
  • 原文地址:https://www.cnblogs.com/csnd/p/12062159.html
Copyright © 2011-2022 走看看