Results 1 to 20 of 20

Thread: Need help getting Initial HDDSize

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    14

    Need help getting Initial HDDSize

    Hey, im not a pro at VB.NET, so please go easy on me.

    Basically i would like to get the Initial Size For the (C Hard drive, and then display it in a textbox. Heres a diagram of what i want.

    Code > Get.(C.Size > hddsize = harddrive size > TextBox2.Text = hddsize. Thankyou!

    EDIT:

    I would also like the code to get the Harddrive name, and display it in a ListBox - Diagram Time xD
    Code > Get.(C.Name > hddname = harddrive name > ListBox1.Text = hddname. Thankyou!
    Last edited by SweetZVB.NET; Nov 27th, 2009 at 08:27 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Need help getting Initial HDDSize

    You should check out the DriveInfo class, which is a member of the System.IO namespace. You can create an instance directly by passing a drive letter to the constructor. You can also get a an array of instances for all drives on the system by calling the Shared GetDrives method. You can then loop through that array and use any or all elements. The DriveInfo class has properties for the values you're looking for.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    14

    Re: Need help getting Initial HDDSize

    Please Elaborate, I am not familiar with VB.NET

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Need help getting Initial HDDSize

    When I provide information like that I generally expect people to use it to do their own research and then make an effort at a solution for themselves. I'm more than happy to help if they have trouble doing that but I would expect to see the attempt first.

    As this is your first post I will cut you a break and provide some code but, for future reference, if I say that you would need to use the DriveInfo class then I would expect that you can go to the MSDN Library documentation and/or the web in general and search for information on that class. Being unfamiliar with VB doesn't preclude you from searching the web.

    Anyway, here's an example of using the DriveInfo class, taken straight from the MSDN documentation:
    Code:
    Imports System
    Imports System.IO
    
    Class Test
        Public Shared Sub Main()
            Dim allDrives() As DriveInfo = DriveInfo.GetDrives()
    
            Dim d As DriveInfo
            For Each d In allDrives
                Console.WriteLine("Drive {0}", d.Name)
                Console.WriteLine("  File type: {0}", d.DriveType)
                If d.IsReady = True Then
                    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)
                End If
            Next
        End Sub
    End Class
    'This code produces output similar to the following:
    '
    'Drive A:\
    '  File type: Removable
    'Drive C:\
    '  File type: Fixed
    '  Volume label: 
    '  File system: FAT32
    '  Available space to current user:     4770430976 bytes
    '  Total available space:               4770430976 bytes
    '  Total size of drive:                10731683840 bytes 
    'Drive D:\
    '  File type: Fixed
    '  Volume label: 
    '  File system: NTFS
    '  Available space to current user:    15114977280 bytes
    '  Total available space:              15114977280 bytes
    '  Total size of drive:                25958948864 bytes 
    'Drive E:\
    '  File type: CDRom
    '
    'The actual output of this code will vary based on machine and the permissions
    'granted to the user executing it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    14

    Re: Need help getting Initial HDDSize

    I cant find System.IO, its not in references, any ideas?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Need help getting Initial HDDSize

    You don't need to find anything. System.IO is a namespace. If your project references any assemblies that contain members of the System.IO namespace then that namespace is available to you. As the DriveInfo class is declared the mscorlib.dll assembly, which every project references by default, the System.IO namespace MUST be available to you.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    14

    Re: Need help getting Initial HDDSize

    Ok, I found out that mscorlib is already added, though, i get "DriveInfo is not Defined"

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Need help getting Initial HDDSize

    Check out the second line of the code I posted.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    14

    Re: Need help getting Initial HDDSize

    I get a "'Imports' statements must precede any declarations."

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Need help getting Initial HDDSize

    Quote Originally Posted by SweetZVB.NET View Post
    I get a "'Imports' statements must precede any declarations."
    Just as the error message says. Notice that the Imports statements are at the very top of the code I posted? That's where Imports statements must be: at the very top of a code file.

    It seems to me that you're missing some of the fundamentals of VB programming. Not a sin, but I'd suggest that that's what you should concentrate on first. Working through a beginner tutorial from start to finish can help with that. Here's a good one:

    http://www.homeandlearn.co.uk/NET/vbNET.html
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    14

    Re: Need help getting Initial HDDSize

    Code:
    Class Test
        Public Shared Sub Main()
            Dim allDrives() As DriveInfo = DriveInfo.GetDrives()
    
            Dim d As DriveInfo
            Dim hdname As String
            For Each d In allDrives
                If d.IsReady = True Then
                    hdname = d.VolumeLabel
                End If
            Next
        End Sub
    End Class
    I tried that, but when i go into the other section of my code, it dosnt recognise the "hdname" variable

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Need help getting Initial HDDSize

    You have declared 'hdname' as a local variable in the Main method. As such, it only exists in the main method. The use of local and member variables and the passing of data from one object to another would fall under the category of fundamentals. I suggest that you work your way through that tutorial I suggested to learn how the language and the Framework work, then go back and try to do things with them, armed with the knowledge of how to do things like get data in one place and use it in another.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    14

    Re: Need help getting Initial HDDSize

    Ive been using that tutorial for a few months now, and at the moment, I am trying to test my knowledge to create a "Fake Windows Format" APP, and it is going perfectly, other than I need to grab the Users (C Label & Size. I really would appreciate more help, and i would be very greatfull. Would it help if i posted the whole source?

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Need help getting Initial HDDSize

    As I said back in post #2:
    You can create an instance directly by passing a drive letter to the constructor.
    If you only and specifically want information about C: drive then there's no point to looping through all the drives on the system. You should simply create one DriveInfo object specifically for the C: drive. You can then get whatever property values you want from that object and do whatever you need to do with them, e.g. display them in a Label on a Form.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    14

    Re: Need help getting Initial HDDSize

    Thats what im looking for, hehe, sorry if i wasnt clear, but all I need is the C: size and Label. I have searched all across the internet, and not much found that i clearly understood, If I were to post my source, would you be able to give me a bit of a tutorial?

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Need help getting Initial HDDSize

    All the information you need has already been provided. You're welcome to post your code if you want but you laready have everything you need.

    1. Create an instance of the DriveInfo class, passing the drive letter of the drive of interest to the constructor

    If you've been working on that Home & Learn tutorial then you know how to create objects and you know how to pass arguments to methods. That's this is.

    2. Once you've got your instance, you get the desired property values from it do with them whatever's appropriate. You've got a code example of that.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  17. #17

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    14

    Re: Need help getting Initial HDDSize

    Ok, I havnt got onto that part yet, so i barely understood that, If i posted my code, would you mind filling it in? I know this is just getting someone else to do my work for me, but I really want to get this done, I know im asking for a big favour. It would be GREATLY appreciated.

  18. #18

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    14

    Re: Need help getting Initial HDDSize

    Bump

  19. #19

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    14

    Re: Need help getting Initial HDDSize

    Bump 2

  20. #20

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    14

    Re: Need help getting Initial HDDSize

    Bump 3.. not very active

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width