locked
How to get disk value in gb RRS feed

  • Question

  • using System;
    using System.Management;
    using System.Windows.Forms;

    namespace WMISample
    {
        public class MyWMIQuery
        {
            public static void Main()
            {
                try
                {
                    ManagementObjectSearcher searcher =
                        new ManagementObjectSearcher("root\\CIMV2",
                        "SELECT * FROM Win32_LogicalDisk");

                    foreach (ManagementObject queryObj in searcher.Get())
                    {
                        Console.WriteLine("-----------------------------------");
                        Console.WriteLine("Win32_LogicalDisk instance");
                        Console.WriteLine("-----------------------------------");
                        Console.WriteLine("Size: {0}", queryObj["Size"]);
                    }
                }
                catch (ManagementException e)
                {
                    MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
                }
            }
        }
    }


    Support@Mytechnet.me

    Tuesday, January 8, 2013 8:05 AM

Answers

  • Hi,

    Put a break point in the following line and check the value of the variable gb.

     sb.Append(string.Format("\nSize in GB: {0}", gb));


    Microsoft MVP - ASP/ASP.NET

    • Proposed as answer by Jason Dot Wang Wednesday, January 9, 2013 7:05 AM
    • Marked as answer by Jason Dot Wang Wednesday, January 16, 2013 8:30 AM
    Wednesday, January 9, 2013 5:56 AM

All replies

  • Hi,

    Try this.

     try
                {
                    ManagementObjectSearcher searcher =
                        new ManagementObjectSearcher("root\\CIMV2",
                        "SELECT * FROM Win32_LogicalDisk");
    
                    StringBuilder sb = new StringBuilder();
    
                    foreach (ManagementObject queryObj in searcher.Get())
                    {
                        sb.Append("\n-----------------------------------");
                        sb.Append("\nWin32_LogicalDisk instance");
                        sb.Append("\n-----------------------------------\n");
                        long bytes = Convert.ToInt64(queryObj["Size"]);
    
                        double gb = bytes / 1024d / 1024d/1024d;
    
                        sb.Append(string.Format("Size in bytes: {0}", bytes));
                        sb.Append(string.Format("\nSize in GB: {0}", gb));
                    }
    
                    MessageBox.Show(sb.ToString());
                }
                catch (ManagementException ex)
                {
                    MessageBox.Show("An error occurred while querying for WMI data: " + ex.Message);
                }

    You can round the value of gb  to the format that you need.


    Microsoft MVP - ASP/ASP.NET

    Tuesday, January 8, 2013 9:07 AM
  • i receive error for StringBuilder

    Support@Mytechnet.me

    Tuesday, January 8, 2013 2:09 PM
  • i did manage by addint this  using  System.Text;

     but dint get any result


    Support@Mytechnet.me

    Tuesday, January 8, 2013 3:53 PM
  • Hi,

    Put a break point in the following line and check the value of the variable gb.

     sb.Append(string.Format("\nSize in GB: {0}", gb));


    Microsoft MVP - ASP/ASP.NET

    • Proposed as answer by Jason Dot Wang Wednesday, January 9, 2013 7:05 AM
    • Marked as answer by Jason Dot Wang Wednesday, January 16, 2013 8:30 AM
    Wednesday, January 9, 2013 5:56 AM
  • Dear thx,

    YET no result


    Support@Mytechnet.me

    Wednesday, January 9, 2013 7:50 AM
  • Hi,

    What do you mean by 'no result'? Do you get zero? How many logical drives are there in your computer?

    Please provide more details.


    Microsoft MVP - ASP/ASP.NET

    Wednesday, January 9, 2013 8:23 AM
  • I got black screen and i have two logical drives


    Support@Mytechnet.me

    Wednesday, January 9, 2013 10:59 AM
  • Hi,

    Did you get zero for the variable 'gb' when you put break point and debug?

    Also post your updated code so that the issue could be identified?


    Microsoft MVP - ASP/ASP.NET

    Wednesday, January 9, 2013 11:20 AM
  • even no 0 value

    here is code

    ===========

    using System;
    using System.Management;
    using System.Windows.Forms;
    using System.IO;
    using System.Text;

    namespace WMISample
    {
        public class MyWMIQuery
        {
            public static void Main()
            {
                try
                {
                    ManagementObjectSearcher searcher =
                        new ManagementObjectSearcher("root\\CIMV2",
                        "SELECT * FROM Win32_LogicalDisk");

                    StringBuilder sb = new StringBuilder();

                    foreach (ManagementObject queryObj in searcher.Get())
                    {
                        sb.Append("-----------------------------------");
                        sb.Append("Win32_LogicalDisk instance");
                        sb.Append("-----------------------------------");
                        long bytes = Convert.ToInt64(queryObj["size"]);
                        double gb = bytes / 1024d / 1024d/1024d;

                        sb.Append(string.Format("Size in bytes: {0}", bytes));
                        sb.Append(string.Format("\nSize in GB: {0}", gb));

                    }
                }
                catch (ManagementException e)
                {
                    MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
                }
            }
        }
    }

     


    Support@Mytechnet.me

    Wednesday, January 9, 2013 12:04 PM
  • Hi,

    You are storing the values to the StringBuilder variable 'sb' but your are not displaying it. Use the following code to display the value of sb in a console application. Use MessageBox.Show() method if your application is a Windows Forms.

    try
                {
                    ManagementObjectSearcher searcher =
                        new ManagementObjectSearcher("root\\CIMV2",
                        "SELECT * FROM Win32_LogicalDisk");
    
                    StringBuilder sb = new StringBuilder();
                    foreach (ManagementObject queryObj in searcher.Get())
                    {
                        Console.WriteLine("-----------------------------------");
                        Console.WriteLine("Win32_LogicalDisk instance");
                        Console.WriteLine("-----------------------------------");
                        Console.WriteLine("Size: {0}", queryObj["Size"]);
    
    
                        sb.Append("\n-----------------------------------");
                        sb.Append("\nWin32_LogicalDisk instance");
                        sb.Append("\n-----------------------------------\n");
                        long bytes = Convert.ToInt64(queryObj["Size"]);
    
                        double gb = bytes / 1024d / 1024d / 1024d;
    
                        sb.Append(string.Format("Size in bytes: {0}", bytes));
                        sb.Append(string.Format("\nSize in GB: {0}", gb));
                    }
                    Console.WriteLine(sb.ToString());
                    Console.ReadKey();  
                }
                catch (ManagementException e)
                {
                    Console.WriteLine("Error occured. " + e.Message);
                }


    Microsoft MVP - ASP/ASP.NET

    Wednesday, January 9, 2013 12:15 PM