Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Determining a pc's memory & disk space

Determining a pc's memory & disk space

Scheduled Pinned Locked Moved C#
performancequestion
5 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Manster
    wrote on last edited by
    #1

    Hi, Can anyone tell me how I can determine how much memory, disk space, and free disk space are on a computer programmatically? This will be displayed on a windows form. I'm thinking it's got to be in one of the dlls that come with windows, but I don't know which one and what functions to use. Thanks! :)

    H J 2 Replies Last reply
    0
    • M Manster

      Hi, Can anyone tell me how I can determine how much memory, disk space, and free disk space are on a computer programmatically? This will be displayed on a windows form. I'm thinking it's got to be in one of the dlls that come with windows, but I don't know which one and what functions to use. Thanks! :)

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Instead of P/Invoking native methods, this is a perfect opportunity to use WMI (see the System.Management namespace). In fact, there's already several great tools to help you. This also allows you to query remote computers where the native functions won't always allow you to do that. VS.NET makes it even easier. Show your Server Explorer tab, expand your computer, then Management Classes, then My Computer and select your computer. You'll notice the property grid has a lot of information you want. Simply drag that to your form and it creates an easy-to-use wrapper class for querying that management information. You can change the computer name and get that information for other computers as well. For disk infomration, you should've noticed the Disk Volumes class right above My Computer. You can create a management class wrapper for that, too, and then specify the path and get the drive's information. You could use the following native functions, but querying information for other computers isn't as easy (GetDiskFreeSpaceEx can use UNC paths, but they must have a trailing backslash (\) and require a share on the machine to query):

      [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
      public static extern bool GetDiskFreeSpaceEx(string root,
      out long freeBytesToCaller,
      out long totalBytes,
      out long freeBytes);
       
      [DllImport("kernel32.dll")]
      public static extern void GlobalMemoryStatus(out MemoryStatus status);
       
      [StructLayout(LayoutKind.Sequential)]
      public struct MemoryStatus
      {
      public int length;
      public int memoryLoad;
      public long totalPhysical;
      public long availablePhysical;
      public long totalPageFile;
      public long availablePageFile;
      public long totalVirtual;
      public long availableVirtual;
      }

      -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

      M 1 Reply Last reply
      0
      • M Manster

        Hi, Can anyone tell me how I can determine how much memory, disk space, and free disk space are on a computer programmatically? This will be displayed on a windows form. I'm thinking it's got to be in one of the dlls that come with windows, but I don't know which one and what functions to use. Thanks! :)

        J Offline
        J Offline
        Jonathan Guidry
        wrote on last edited by
        #3

        Look at the System.Management namespace ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\""); disk.Get(); object freeSpace = disk.Properties["FreeSpace"].Value; Jonathan

        1 Reply Last reply
        0
        • H Heath Stewart

          Instead of P/Invoking native methods, this is a perfect opportunity to use WMI (see the System.Management namespace). In fact, there's already several great tools to help you. This also allows you to query remote computers where the native functions won't always allow you to do that. VS.NET makes it even easier. Show your Server Explorer tab, expand your computer, then Management Classes, then My Computer and select your computer. You'll notice the property grid has a lot of information you want. Simply drag that to your form and it creates an easy-to-use wrapper class for querying that management information. You can change the computer name and get that information for other computers as well. For disk infomration, you should've noticed the Disk Volumes class right above My Computer. You can create a management class wrapper for that, too, and then specify the path and get the drive's information. You could use the following native functions, but querying information for other computers isn't as easy (GetDiskFreeSpaceEx can use UNC paths, but they must have a trailing backslash (\) and require a share on the machine to query):

          [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
          public static extern bool GetDiskFreeSpaceEx(string root,
          out long freeBytesToCaller,
          out long totalBytes,
          out long freeBytes);
           
          [DllImport("kernel32.dll")]
          public static extern void GlobalMemoryStatus(out MemoryStatus status);
           
          [StructLayout(LayoutKind.Sequential)]
          public struct MemoryStatus
          {
          public int length;
          public int memoryLoad;
          public long totalPhysical;
          public long availablePhysical;
          public long totalPageFile;
          public long availablePageFile;
          public long totalVirtual;
          public long availableVirtual;
          }

          -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

          M Offline
          M Offline
          Manster
          wrote on last edited by
          #4

          Heath, Thanks for your help, but I can not find the System.Management namespace. Where is this namespace located and or how do I add it? I tried using System.Management; but that doesn't work. Thanks

          M 1 Reply Last reply
          0
          • M Manster

            Heath, Thanks for your help, but I can not find the System.Management namespace. Where is this namespace located and or how do I add it? I tried using System.Management; but that doesn't work. Thanks

            M Offline
            M Offline
            Manster
            wrote on last edited by
            #5

            Sorry, I figured out that I didn't add the reference. Hey, it's a Monday morning. Thanks

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups