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 in Visual Basic 2005
DescriptionThis example shows how to use the fsutil utility to get a disk's NTFS information 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. It uses the following code to load the list when the program starts.

 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    For Each drive_info As DriveInfo In _
        DriveInfo.GetDrives()
        lstDrives.Items.Add(drive_info.Name)
    Next drive_info
End Sub
 
When you click a drive in the list, the following code calls function GetNtfsInfo.
 
Private Sub lstDrives_SelectedIndexChanged(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    lstDrives.SelectedIndexChanged
    Dim drive_info As New DriveInfo(lstDrives.Text)
    txtResults.Text = GetNtfsInfo(lstDrives.Text)
    txtResults.Select(0, 0)
End Sub
 
Function GetNtfsInfo uses a Process object to execute the program fsutil passing it the command line parameters "fsinfo ntfsinfo" followed by the selected drive's name. It captures the process's output and returns it in a string so the calling routine can display it in a text box.
 
Private Function GetNtfsInfo(ByVal drive_name As String) As _
    String
    Dim result As String = ""

    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.
        result = std_out.ReadToEnd() & std_err.ReadToEnd()

        ' Clean up.
        std_out.Close()
        std_err.Close()
        proc.Close()
    Catch ex As Exception
        result = "Error getting information" & vbCrLf & _
            vbCrLf & ex.Message
    End Try

    Return result
End Function
 
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
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated