Skip to main content

Get Free Disk Space on Server

We can determine Free Disk Space on Server using WMI provider. In this article we will use GetFreeDiskSpace Function which is written in VB.Net and you can use it in your project.

Function Parameter
  • Target Computer Name
  • Admin Credential(Username/Password)
Here we used Win32_LogicalDisk Class for querying Hard-Disk information.

Hard-Disk Information which we will gather is given below:
  • Drive Letter
  • File System
  • Total Space (GB)
  • Free Space (GB)
Source Code for Free Disk Space on Server in VB.Net

Public Sub GetFreeDiskSpace(ByVal strComputer As String, ByVal strUserName As String, ByVal strPwd As String)

        Dim query As Management.ManagementObjectSearcher
        Dim queryCollection As Management.ManagementObjectCollection
        Dim management_object1 As Management.ManagementObject
        Dim msc As Management.ManagementScope

        If (strComputer = System.Environment.MachineName) Then
            msc = New Management.ManagementScope("\\" & strComputer & "\root\cimv2")
        Else
            Dim co As New Management.ConnectionOptions
            co.Username = strUserName
            co.Password = strPwd
            msc = New Management.ManagementScope("\\" & strComputer & "\root\cimv2", co)
        End If

        Dim query_command As String = "SELECT * FROM Win32_LogicalDisk"
        Dim select_query As Management.SelectQuery = New Management.SelectQuery(query_command)

        Try
            query = New Management.ManagementObjectSearcher(msc, select_query)
            queryCollection = query.Get()
            Dim strDisk As String = ""
            For Each management_object1 In queryCollection

                strDisk = "Drive Letter: " & management_object1("DeviceID") & vbCrLf & "File System: " & management_object1("FileSystem") & vbCrLf & "Total Space: " & ((CType(management_object1("Size"), String) / 1024) / 1024) / 1024 & " GB" & vbCrLf & "Free Space: " & ((CType(management_object1("FreeSpace"), String) / 1024) / 1024) / 1024 & " GB"

                MessageBox.Show(strDisk)
            Next management_object1
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try

End Sub

Popular posts from this blog

Ping Network Computer in VB.Net

"Ping" is very basic utility for every network programmer. Here we will discuss ping a computer in Network using VB.Net. We have two methods for pinging a computer. Method 1: Here we will use VB.Net NetworkInformation NameSpace. Public Sub PingStatus(ByVal strComputer As String) Dim pingSender As Ping = New Ping() Dim options As PingOptions = New PingOptions() options.DontFragment = True 'Create a buffer of 32 bytes of data to be transmitted Dim data As String = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" Dim buffer As Byte() = Encoding.ASCII.GetBytes(data) Dim timeout As Integer = 120 Dim reply As PingReply = pingSender.Send(strComputer, timeout, buffer, options) If (reply.Status = IPStatus.Success) Then MessageBox.Show("Ping Successed.") Else MessageBox.Show("Ping Faild.") End If End Sub Method 2: Here we are using .Net inbuil

GridView Paging and Sorting

Introduction Paging and Sorting are most commonly used features of ASP.Net GridView. And it is very easy to implement in GridView with small lines of code. Here I am going to demonstrate how to use Paging and Sorting in GridView for better use of data display.