9 Replies - 506 Views - Last Post: 08 February 2013 - 02:21 AM Rate Topic: -----

#1 oyyou  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 179
  • Joined: 26-April 10

Getting free space on Hard Drive

Posted 07 February 2013 - 03:24 AM

Okay, so I'm creating an application.
The point of the application is to get the free space on a specified hard drive (The user types the letter) and it's represented with a graph of some kind.

What I'm stuck with is trying to determine if the hard drive is valid or not.
For example, somebody could type "e", but that isn't a hard drive, so they get a warning.

Here is what I'm using to find hard drive.

            string letter;
            letter = Console.ReadLine();

            ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"" + letter + ":\"");



So I'm just looking for some sort of validation.

Tah (:

Is This A Good Question/Topic? 0
  • +

Replies To: Getting free space on Hard Drive

#2 bgammill  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 39
  • Joined: 11-June 12

Re: Getting free space on Hard Drive

Posted 07 February 2013 - 04:21 AM

I posted a reply to a similar question a couple of weeks ago. We can use what I posted to help solve this problem.

I find it easier to use the ManagementObjectSearcher class. It allows you to do queries for specific hardware and properties. The Win32_LogicalDisk class has the property FreeSpace; I'll be using that in my query.
ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select FreeSpace from Win32_LogicalDisk WHERE DeviceID=\"" + letter + ":\"");
ManagementObjectCollection collection = searcher.Get();



Once that code runs you can take the collection and loop through each ManagementObject, and then through each PropertyData. While you're doing that, you can check if the PropertyData property Value is null. If the disk is valid, it won't be null.
            foreach (ManagementObject obj in collection)
            {
                foreach (PropertyData prop in obj.Properties)
                {
                    if (prop.Value == null)
                    {
                        Console.WriteLine("Disk not found!");
                        Console.ReadLine();
                        return;
                    }
                    Console.WriteLine(prop.Value);
                    Console.ReadLine();
                }
            }


Was This Post Helpful? 2
  • +
  • -

#3 oyyou  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 179
  • Joined: 26-April 10

Re: Getting free space on Hard Drive

Posted 07 February 2013 - 04:36 AM

That is brilliant! Thank you.
Works like a charm.

I'm going to mold it to fit in a gui, but you've done the hard bit.

Again, thank you (:
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,354
  • Joined: 02-June 10

Re: Getting free space on Hard Drive

Posted 07 February 2013 - 08:22 AM

I love that solution. But like to show options as well.
What about something as simple as trying to get a list of files from the given drive? If it throws and exception then the drive doesn't exist.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2875
  • View blog
  • Posts: 9,538
  • Joined: 12-December 12

Re: Getting free space on Hard Drive

Posted 07 February 2013 - 09:20 AM

What is wrong with:
Edited: Actually, the following doesn't confirm if it's a hard drive, just that it exists; but you could then check DriveInfo.DriveType.

            if (Directory.Exists("C:")) {
                MessageBox.Show("You have C drive!");
            }

Then you might use DriveInfo to find the AvailableFreeSpace:

            DriveInfo[] allDrives = DriveInfo.GetDrives();

            foreach (DriveInfo d in allDrives) {
                Console.WriteLine("Drive {0}", d.Name);
                Console.WriteLine("  File type: {0}", d.DriveType);
                if (d.IsReady == true) {
                    Console.WriteLine("  Volume label: {0}", d.VolumeLabel);
                    Console.WriteLine("  File system: {0}", d.DriveFormat);
                    Console.WriteLine(
                        "  Available space to current user:{0, 15} bytes",
                        d.AvailableFreeSpace);

                    Console.WriteLine(
                        "  Total available space:          {0, 15} bytes",
                        d.TotalFreeSpace);

                    Console.WriteLine(
                        "  Total size of drive:            {0, 15} bytes ",
                        d.TotalSize);
                }
            }

MSDN

This post has been edited by andrewsw: 07 February 2013 - 09:24 AM

Was This Post Helpful? 2
  • +
  • -

#6 oyyou  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 179
  • Joined: 26-April 10

Re: Getting free space on Hard Drive

Posted 07 February 2013 - 10:01 AM

View PosttlhIn`toq, on 07 February 2013 - 08:22 AM, said:

What about something as simple as trying to get a list of files from the given drive?


That's a good idea. I may try using that solution above me and see what I can get.
I'm just making different versions at the minute. Seeing what works best.

Also, I really like this
            if (Directory.Exists("C:")) {
                MessageBox.Show("You have C drive!");
            }


It works well since my current code was detecting disk drives and doing nothing about it.
So thank you.

This post has been edited by oyyou: 07 February 2013 - 10:05 AM

Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2875
  • View blog
  • Posts: 9,538
  • Joined: 12-December 12

Re: Getting free space on Hard Drive

Posted 07 February 2013 - 10:08 AM

If you only want to obtain the free space you can short-circuit:

            long CFree = 0;

            bool CDriveExists = DriveInfo.GetDrives().Any(delegate(DriveInfo di) {
                if (di.Name == "C:\\" && (di.DriveType).ToString() == "Fixed") {
                    CFree = di.AvailableFreeSpace;
                    return true;
                } else {
                    return false;
                }
            });

            if (CDriveExists) {
                MessageBox.Show(String.Format("Yes it does!\nFree: {0}", CFree));
            }

Modify the drive-letter :)

This post has been edited by andrewsw: 07 February 2013 - 10:09 AM

Was This Post Helpful? 0
  • +
  • -

#8 oyyou  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 179
  • Joined: 26-April 10

Re: Getting free space on Hard Drive

Posted 07 February 2013 - 10:16 AM

View Postandrewsw, on 07 February 2013 - 10:08 AM, said:

If you only want to obtain the free space you...


I'm making an application that's to show the total memory, used memory, and free memory of the selected hard drive.
The user inputs a driver letter (It's default to C) and then the hard drive info is to be shown in a pie-chart.

I think the application is aimed for people who aren't too computer literate.


I've done all the logic behind it now. I'm just making sure it looks pretty, and maybe finding a better way to create pie-charts.
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,354
  • Joined: 02-June 10

Re: Getting free space on Hard Drive

Posted 07 February 2013 - 10:56 AM

Is this just an exercise for practice and learning?
Because there is already a ton of these in addition to Task Manager.
And most don't require any interaction on the part of the user, making them more friendly.

http://www.computerw...itoring_your_PC

http://pcsupport.abo...info-gadget.htm
Was This Post Helpful? 0
  • +
  • -

#10 oyyou  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 179
  • Joined: 26-April 10

Re: Getting free space on Hard Drive

Posted 08 February 2013 - 02:21 AM

View PosttlhIn`toq, on 07 February 2013 - 10:56 AM, said:

Is this just an exercise for practice and learning?


This is my first task as Junior Developer. I think it's to give me some experience, as well as make something that can be used inside the company I work for.
It's pretty much completed now, I'm just making it look pretty.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1