?
Solved

disk space

Posted on 2004-09-09
5
Medium Priority
?
534 Views
Last Modified: 2012-06-27
I'm trying to figure out how much disk space is on a network drive.  I have found several examples but none of them give me the correct size.

I used the info  obtained from this question:
http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_20747539.html?query=check+disk+space+and+C&clearTAFilter=true&showAll=true

The code gives me disk space info, but what I don't understand is the number don't match up at all.  The code says I had two drives a (C: and D:) which is correct, but then the code outputed that C: was 125GB and had 35GB free, the D: drive supposely was 23Gb drive with 11Gbs free.  None of this info is even closely correct.  C drive is a 20Gb drive with 13GB free and D drive is 450GB with 48GB free.  

I also tried this on several other network drives and my local drive...and I got the SAME INDENTICAL NUMBERS as stated a above.
Can some one shed some light on this subject.  What am I not seeing or not doing correctly????


Thanks!


CODE:

ConnectionOptions oConn = new ConnectionOptions();
long gb = 134217728; //gigabyte
 
System.Management.ManagementScope oMs = new System.Management.ManagementScope("\\\\na0029", oConn);    

//get Fixed disk stats
System.Management.ObjectQuery oQuery = new System.Management.ObjectQuery("select FreeSpace,Size,Name from Win32_LogicalDisk where DriveType=3");

//Execute the query  
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs,oQuery);

//Get the results
ManagementObjectCollection oReturnCollection = oSearcher.Get();  
         
//loop through found drives and write out info
foreach( ManagementObject oReturn in oReturnCollection )
{
     // Disk name
     lw.WriteLine("Name : " + oReturn["Name"].ToString());
     // Free Space in bytes
     long ndiv = Convert.ToInt64(oReturn["FreeSpace"])/gb;
     lw.WriteLine("FreeSpace : " + ndiv.ToString());
     // Size in bytes
     ndiv = Convert.ToInt64(oReturn["Size"])/gb;
     lw.WriteLine("Size : " + ndiv);
}

OUTPUT:
Name : C:
FreeSpace : 35 GB
Size : 125 GB
Name : D:
FreeSpace : 11 GB
Size : 23 GB



0
Comment
Question by:mrwhipple
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 4

Accepted Solution

by:
NTAC earned 2000 total points
ID: 12020389
Use this code:

************************
ConnectionOptions oConn = new ConnectionOptions();
double gb = 1073741824.0; //gigabyte
 
System.Management.ManagementScope oMs = new System.Management.ManagementScope("\\\\na0029", oConn);    

//get Fixed disk stats
System.Management.ObjectQuery oQuery = new System.Management.ObjectQuery("select FreeSpace,Size,Name from Win32_LogicalDisk where DriveType=3");

//Execute the query  
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs,oQuery);

//Get the results
ManagementObjectCollection oReturnCollection = oSearcher.Get();  
         
//loop through found drives and write out info
foreach( ManagementObject oReturn in oReturnCollection )
{
     // Disk name
       Console.WriteLine("Name : " + oReturn["Name"].ToString());
       // Free Space in bytes
       double ndiv = Convert.ToInt64(oReturn["FreeSpace"])/gb;
       Console.WriteLine("FreeSpace : " + ndiv.ToString());
       // Size in bytes
       ndiv = Convert.ToInt64(oReturn["Size"])/gb;
       Console.WriteLine("Size : " + ndiv);
}

**********************

You choice of a gigabyte was wrong.  Also, use a double or a decimal so you can keep the decimal place.  You can change the formatting because it outputs 10 decimal places.

1 GB = 1024*1024*1024 (1 kilobyte = 1024 bytes).

Regards,
NTAC
0
 
LVL 1

Author Comment

by:mrwhipple
ID: 12021436
Thanks for the quick reply,

I get the right numbers when I test my local machine, but it seems when I try and access a network machine I get the same values.  Does this code not work when accessing a machine that is not locally?

Does the path matter?  the machine name is na0029, I get no errors when using it : (ie)
System.Management.ManagementScope oMs = new System.Management.ManagementScope("\\\\na0029", oConn);    
I've tried adding to the path: for example add the shared folder on to the path, but all that does is error out on the above line.


Thanks.

0
 
LVL 4

Expert Comment

by:NTAC
ID: 12021815
With my mapped drives, I can see them when I change the DriveType=4 (networked drives).  I'm not sure how your system is set up.  Do  you have a mapped drive to the other computer, or is it just a folder?  How is it set up?
0
 
LVL 1

Author Comment

by:mrwhipple
ID: 12022008
I have it setup as a mapped drive.

I get the info when I change my DriveType=4, excellent, is it possible to do it with out the drive being mapped?  Reason I asked is because this code will be part of a program that is on 200+ processing machines that are able to access 30+ network storage machines.  So it will be cumbersome if I have to map all the network storage machines on all 200+ machines!


Thanks.



   
0
 
LVL 4

Expert Comment

by:NTAC
ID: 12023363
mrwhipple

Now you are asking a different question.  I guess a way you could do it is to query the name of all computers on the network (or a list of them that you already know)--and then use the above code to retreive the local drive(s) on the system.  If you happen to know the setup of each of the storage systems, it might be a bit easier.

Regards,
NTAC
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question