Diskinfo

hendrikbez

Active member
Joined
Nov 24, 2005
Messages
35
Location
South Afica
Programming Experience
1-3
Here are my code(with help). How can I do the following.

1. How can I show the bytes in this format 123,123,123,123 bytes.

2. How can I use this code to get my servers drive info(I have the user names and passwords for all of them.

VB.NET:
Imports System.IO ' for readig text and get driveinfo
Imports System.Net.NetworkInformation 'ip addrsss/ mask eg.
Public Class My_Pc

    'Private Declare Function GetDiskFreeSpaceEx Lib "kernel32" Alias "GetDiskFreeSpaceExA" (ByVal lpDirectoryName As String, ByRef lpFreeBytesAvailableToMe As Long, ByRef lpTotalNumberOfBytes As Long, ByRef lpTotalNumberOfFreeBytes As Long) As Integer

    Private Sub BtnInfo_ClickButtonArea(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles BtnInfo.ClickButtonArea

        ' To get host name
        Dim ipProperties As IPGlobalProperties = IPGlobalProperties.GetIPGlobalProperties()
        Dim myProcess As New Process
        Dim myProcessStartInfo As New ProcessStartInfo("ipconfig", "/all")

        ' to use text file
        Dim objStreamWriter As StreamWriter

        'Get disk info
        Dim allDrives() As DriveInfo = DriveInfo.GetDrives()
        Try
            Dim d As DriveInfo

            For Each d In allDrives
                objStreamWriter = New StreamWriter("C:\Werk\Disk info.txt", True)

                If d.DriveType.ToString = "Fixed" Then

                    If d.IsReady = True Then
                        objStreamWriter.WriteLine(" ")
                        objStreamWriter.WriteLine(ipProperties.HostName + "  " + d.Name.ToString + "  " + d.AvailableFreeSpace.ToString + " " + " Bytes free")
                        lstdrives.Items.Add(ipProperties.HostName + "  " + d.Name + "  " + d.AvailableFreeSpace.ToString + " " + " Bytes free")
                    End If
                End If

                'Close the file.
                objStreamWriter.Close()
            Next
            MsgBox("Write Successful")
        Catch er As Exception
            MsgBox(er.Message.ToString, , "Unable to write to file")
        End Try

    End Sub
End Class
 
Try:
d.AvailableFreeSpace.ToString("n"c)

instead of:
d.AvailableFreeSpace.ToString

Also, why are you opening the file, writing to it, then closing the file inside the loop?
Things would run much more efficiently if you were to open the file, then loop through all of the drives on the system doing all of the writing there, then after the loop finishes close the file.
 
JuggaloBrotha thank you for your replay.

I am a beginner in vb.net, so can you please explain to me what you mean with this "what is the N and C in the following code. I Have get this to work now :p , but it shows 123,123,123.00 How can I get the .00 out. :eek:

d.AvailableFreeSpace.ToString("n"c) Got it working

Can you explain the writing and loop thing for me.

Thank you again
 
Last edited:
The loop he is talking about is the "For... Next" loop. Inside that loop you are opening the file, writing a line, and then closing the file. Aka... if you have a thousand lines to write to it, you are opening/closing the file a thousand times.

If you move your open statement to just before the "For" statement and move your close statement to just after the "Next", the program becomes more efficient. Aka... you open the file (once), you write a thousand lines, you close the file (once).;)
 
JuggaloBrotha thank you for your replay.

I am a beginner in vb.net, so can you please explain to me what you mean with this "what is the N and C in the following code. I Have get this to work now :p , but it shows 123,123,123.00 How can I get the .00 out. :eek:

d.AvailableFreeSpace.ToString("n"c) Got it working

Can you explain the writing and loop thing for me.

Thank you again

The "n" is a string parameter telling the .ToString method that you want this number formatted as a number when the string is returned and since the ToString method only accepts a char, I put the c after the "n" to let the compiler know that it's a char and not a string.

This will remove the ".00" from the end of the string:
d.AvailableFreeSpace.ToString("n"c).Replace(".00", String.Empty)

As for the loop thing, ggunter's explained it already and here's an example:
VB.NET:
        Try
            Dim d As DriveInfo

            objStreamWriter = New StreamWriter("C:\Werk\Disk info.txt", True)
            For Each d In allDrives
                If d.DriveType.ToString = "Fixed" Then

                    If d.IsReady = True Then
                        objStreamWriter.WriteLine(" ")
                        objStreamWriter.WriteLine(ipProperties.HostName + "  " + d.Name.ToString + "  " + d.AvailableFreeSpace.ToString + " " + " Bytes free")
                        lstdrives.Items.Add(ipProperties.HostName + "  " + d.Name + "  " + d.AvailableFreeSpace.ToString + " " + " Bytes free")
                    End If
                End If
            Next
            'Close the file.
            objStreamWriter.Close()

            MsgBox("Write Successful")
        Catch er As Exception
            MsgBox(er.Message.ToString, , "Unable to write to file")
        End Try
 
Back
Top