Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse the fsutil utility to get a disk's NTFS information into an object with fields in Visual Basic 2005
DescriptionThis example shows how to use the fsutil utility to get a disk's NTFS information into an object with fields in Visual Basic 2005.
Keywordsfsutil, NTFS, console, VB2005, VB.NET
CategoriesWindows, Software Engineering, VB.NET
 
The NTFS information includes such things as the drive's NTFS version number, number of sectors, number of free sectors, and so forth.

This program displays a list of the computer's drives in a ListBox. When you select a disk, the program calls the following GetNtfsInfo function.

This function uses a Process object to execute the program fsutil passing it the command line parameters "fsinfo ntfsinfo" followed by the selected drive's name. The result looks like this:

NTFS Volume Serial Number :       0x1688b54988b527df
Version :                         3.1
Number Sectors :                  0x0000000009e5b05f
Total Clusters :                  0x00000000013cb60b
Free Clusters  :                  0x0000000000983142
Total Reserved :                  0x0000000000000000
Bytes Per Sector  :               512
Bytes Per Cluster :               4096
Bytes Per FileRecord Segment    : 1024
Clusters Per FileRecord Segment : 0
Mft Valid Data Length :           0x0000000004ce0000
Mft Start Lcn  :                  0x00000000000c4fd2
Mft2 Start Lcn :                  0x00000000003e823e
Mft Zone Start :                  0x0000000000206340
Mft Zone End   :                  0x0000000000212220

The program captures the output in a string and parses the result. It uses Split to break the result into lines and then uses Split again to break each line at the colon character. It uses a Select Case statement to identify each field and fills in the correct property in the result object.

 
Private Function GetNtfsInfo(ByVal drive_name As String) As _
    NtfsInfo
    Dim text_info As String = ""
    Dim result As New NtfsInfo

    Try
        ' Set start information.
        If drive_name.EndsWith("\") Then drive_name = _
            drive_name.Substring(0, drive_name.Length - 1)
        Dim start_info As New ProcessStartInfo("fsutil", " " & _
            "fsinfo ntfsinfo " & drive_name)
        start_info.UseShellExecute = False
        start_info.CreateNoWindow = True
        start_info.RedirectStandardOutput = True
        start_info.RedirectStandardError = True

        ' Make the process and set its start information.
        Dim proc As New Process()
        proc.StartInfo = start_info

        ' Start the process.
        proc.Start()

        ' Attach to stdout and stderr.
        Dim std_out As StreamReader = proc.StandardOutput()
        Dim std_err As StreamReader = proc.StandardError()

        ' Display the results.
        text_info = std_out.ReadToEnd() & _
            std_err.ReadToEnd()

        ' Clean up.
        std_out.Close()
        std_err.Close()
        proc.Close()

        ' Parse the results.
        text_info = text_info.Replace(vbLf, _
            "").Replace(vbCr & vbCr, vbCr)
        Dim lines() As String = text_info.Split(vbCr)
        For Each one_line As String In lines
            If one_line.Length > 0 Then
                Dim property_name As String = _
                    one_line.Split(":")(0).Replace(" ", "")
                Dim property_value As String = _
                    one_line.Split(":")(1).Replace(" ", "")
                Select Case property_name
                    Case "NTFSVolumeSerialNumber"
                        result.NTFSVolumeSerialNumber = _
                            property_value
                    Case "Version"
                        result.Version = property_value
                    Case "NumberSectors"
                        result.NumberSectors = _
                            property_value
                    Case "TotalClusters"
                        result.TotalClusters = _
                            property_value
                    Case "FreeClusters"
                        result.FreeClusters = property_value
                    Case "TotalReserved"
                        result.TotalReserved = _
                            property_value
                    Case "BytesPerSector"
                        result.BytesPerSector = _
                            property_value
                    Case "BytesPerCluster"
                        result.BytesPerCluster = _
                            property_value
                    Case "BytesPerFileRecordSegment"
                        result.BytesPerFileRecordSegment = _
                            property_value
                    Case "ClustersPerFileRecordSegment"
                        result.ClustersPerFileRecordSegment _
                            = property_value
                    Case "MftValidDataLength"
                        result.MftValidDataLength = _
                            property_value
                    Case "MftStartLcn"
                        result.MftStartLcn = property_value
                    Case "Mft2StartLcn"
                        result.Mft2StartLcn = property_value
                    Case "MftZoneStart"
                        result.MftZoneStart = property_value
                    Case "MftZoneEnd"
                        result.MftZoneEnd = property_value
                End Select
            End If
        Next one_line

    Catch ex As Exception
        text_info = "Error getting information" & vbCrLf & _
            vbCrLf & ex.Message
    End Try

    Return result
End Function
 
The program finishes by displaying each of the fields in a text box.
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated