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

  • 相关阅读:
    2.15 STL复习
    20190214Test(栈与队列)
    STL列表链式前向星
    链式前向星(邻接表)
    Priority_queue详解
    List详解
    NOIP2019计划
    第二章笔记
    第一章笔记
    本地文件上传GitHub
  • 原文地址:https://www.cnblogs.com/csnd/p/12062159.html
Copyright © 2011-2022 走看看