UNC Information from VB.NET

How can I obtain disk information (total size, free,  etc...) given the UNC for a network share using VB.NET?

LVL 1
Howard BashSenior Software EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CodeCruiserCommented:
I doubt you can do that as even Windows does not show the size of a network share.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
spprivateCommented:
Use this and call TotalSpace and TotalFreeSpace properties

http://windevblog.blogspot.com/2008/09/get-disk-drive-type-with-vbnet.html
vb_studentCommented:
you can use code like this

Dim drive As System.IO.DriveInfo = My.Computer.FileSystem.GetDriveInfo("//UNCPath")
dim freeSpace as long = drive.AvailableFreeSpace
the my.computer.filesystem namespace has quite a few functions you can use
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

spprivateCommented:
Here is the code from above link I modified to get total space and free space.The deal is to use
mpr.dll in this API
 Public Declare Function WNetGetConnection Lib "mpr.dll" Alias "WNetGetConnectionA" _
 (ByVal lpszLocalName As String, ByVal lpszRemoteName As String, ByRef cbRemoteName As Integer) As Integer

I used VS 2008 and VB.Net for this
Imports System.IO
Public Class Form1
    Public Declare Function WNetGetConnection Lib "mpr.dll" Alias "WNetGetConnectionA" _
 (ByVal lpszLocalName As String, ByVal lpszRemoteName As String, ByRef cbRemoteName As Integer) As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
    Public Function GetNetDriveName(ByVal DriveLetter As String) As String
        '
        ' Return mapped drive UNC name
        Dim RetVal As Integer
        Dim OutName As String = New String(CChar(" "), 260)
        Dim NameLength As Integer = 260

        Try
            RetVal = WNetGetConnection(DriveLetter, OutName, NameLength)
            OutName = OutName.Replace(Chr(0), " ").TrimEnd(CChar(" "))
            Return OutName
        Catch ex As Exception
            Return ""
        End Try
    End Function
    Public Sub GetDrives(ByRef DriveLetter() As String, ByRef VolumeLabel() As String, _
  ByRef DriveTypeVal() As DriveType, ByRef PathToDrive() As String, _
  ByRef IsDriveReady() As Boolean, ByRef TotalSpace() As Long, ByRef FreeSpace() As Long)
        '
        ' Return available disc drives
        '
        Const DRIVELETTERS As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        Dim Info As DriveInfo
        Dim Count As Integer
        Dim i As Integer

        Try
            Count = DRIVELETTERS.Length - 1
            ReDim DriveLetter(Count)
            ReDim VolumeLabel(Count)
            ReDim DriveTypeVal(Count)
            ReDim PathToDrive(Count)
            ReDim IsDriveReady(Count)
            ReDim TotalSpace(Count)
            ReDim FreeSpace(Count)
            For i = 0 To Count
                DriveLetter(i) = DRIVELETTERS.Substring(i, 1)
                VolumeLabel(i) = ""
                DriveTypeVal(i) = DriveType.Unknown
                PathToDrive(i) = ""
                IsDriveReady(i) = False
            Next i
            For Each Info In My.Computer.FileSystem.Drives
                Count = DRIVELETTERS.IndexOf(Info.RootDirectory.FullName.Substring(0, 1))
                DriveTypeVal(Count) = Info.DriveType

                If Info.IsReady Then
                    IsDriveReady(Count) = True
                    VolumeLabel(Count) = Info.VolumeLabel
                    TotalSpace(Count) = Info.TotalSize()
                    FreeSpace(Count) = Info.TotalFreeSpace()
                Else
                    IsDriveReady(Count) = False
                    VolumeLabel(Count) = ""
                End If
                If Info.DriveType = DriveType.Network Then
                    PathToDrive(Count) = GetNetDriveName(DriveLetter(Count) & ":")
                Else
                    PathToDrive(Count) = DriveLetter(Count) & ":"
                End If
            Next Info
        Catch ex As Exception
            ' Error handling
        End Try

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '
        ' Get drives and their types
        Dim DriveLetter() As String
        Dim VolumeLabel() As String
        Dim DriveTypeVal() As DriveType
        Dim PathToDrive() As String
        Dim IsDriveReady() As Boolean
        Dim TotalSpace() As Long
        Dim FreeSpace() As Long
        Dim TempStr As String
        Dim i As Integer

        Try
            ReDim DriveLetter(0)
            ReDim VolumeLabel(0)
            ReDim DriveTypeVal(0)
            ReDim PathToDrive(0)
            ReDim IsDriveReady(0)
            ReDim TotalSpace(0)
            ReDim FreeSpace(0)
            GetDrives(DriveLetter, VolumeLabel, DriveTypeVal, PathToDrive, IsDriveReady, TotalSpace, FreeSpace)
            ListBox1.Items.Clear()
            TempStr = "DriveLetter" & "; "
            TempStr = TempStr & "DriveType" & "; "
            TempStr = TempStr & "VolumeLabel" & "; "
            TempStr = TempStr & "PathToDrive" & "; "
            TempStr = TempStr & "TotalSpace" & "; "
            TempStr = TempStr & "FreeSpace" & "; "
            TempStr = TempStr & "IsDriveReady"
            ListBox1.Items.Add(TempStr)
            For i = 0 To DriveLetter.GetUpperBound(0)
                TempStr = DriveLetter(i) & "; "
                TempStr = TempStr & DriveTypeVal(i).ToString & "; "
                TempStr = TempStr & VolumeLabel(i) & "; "
                TempStr = TempStr & PathToDrive(i) & "; "
                TempStr = TempStr & TotalSpace(i) & "; "
                TempStr = TempStr & FreeSpace(i) & "; "
                TempStr = TempStr & IsDriveReady(i).ToString
                ListBox1.Items.Add(TempStr)
            Next i
        Catch ex As Exception
            ' Error handling
        End Try

    End Sub
End Class

Open in new window

Howard BashSenior Software EngineerAuthor Commented:
The above code I think will iterate all the drives that are mapped on my PC,  however the problem is that I have many many shares to retrieve this information from and I do not want to map all the shares.  

What I need is a function that will accept a UNC such as \\SomeSharedServer\C$ and return total size, free space, etc.
vb_studentCommented:
i don't know if this code will work on a share with the C$ at the end but the code i gave above could be used like this
Dim drive As System.IO.DriveInfo = My.Computer.FileSystem.GetDriveInfo("\\SomeSharedServer\C$")
dim freeSpace as long = drive.AvailableFreeSpace
the problom is that drives found by that method usaly reqiure a password to access. i have don't know how visual basic will handle this. but try that code  if you get access this way you can read and even write files on that drive not just rad free space. i hope this helps.
spprivateCommented:
There is a round about way if you are desperate.
You can map a network drive dynamically and get the details of the drive,then disconnect.
But this assumes that you have access persmisson on all the shares
spprivateCommented:
Here is the modified code for this.
I first disconnect a drive if it exists.
Create a Map
Get details
Disconnect the map.
This assumes the user or the service account as rights in all the shared paths

Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszPassword As String, ByVal lpszLocalName As String) As Long
    Private Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias "WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As Long, ByVal fForce As Long) As Long
    Private Const CONNECT_UPDATE_PROFILE = &H1


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim rcDisconnect As Integer
        Dim Si As Long
        Dim Fr As Long
        rcDisconnect = WNetCancelConnection2("Y:", 0, True) '//Disconnect the drive if already there.You can modify this and avoid the step if drive exist
        If WNetAddConnection("\\10.216.99.13\C$", "", "Y:") = 0 Then 'You can pass the network share,password if any and the letter you want to assign and create the Map temporarly
            For Each Info In My.Computer.FileSystem.Drives

                If Info.Name = "Y:\" Then 'What ever drive name you pass

                    If Info.IsReady Then

                        Si = Info.TotalSize() 'Long value returned.Divide by 1024 to get KB,MB or GB values.
                        Fr = Info.TotalFreeSpace() 'Long value returned.Divide by 1024 to get KB,MB or GB values.

                    End If
                End If

            Next Info

        End If
        rcDisconnect = WNetCancelConnection2("Y:", 0, True) ' Disconnect once done.

    End Sub

Open in new window

Howard BashSenior Software EngineerAuthor Commented:
I saw that you can get the information by letter and that implied that I might be able to get such information if I mapped the UNC path and then passed the letter.  For some reason,  there appears to be no way to go directly and get the info from the UNC.

I wrote a routine to map and unmap shares, but as you noted, VB Student,  I need rights to the shares (uid/pwd) and would need to use these to grant rights to execute the function upon.  It works,  but that I need to map/unmap to obtain a set of paths seems like a waste of bandwidth.
CodeCruiserCommented:
>there appears to be no way to go directly and get the info from the UNC.
That reaffirms my comment above.
Howard BashSenior Software EngineerAuthor Commented:
I'd like to hang in on the question for another day or so and see if anyone else has another avenue to check.
spprivateCommented:
No you misunderstood.
The letter you assign is a temporary drive letter assigned by the program .
Here is the way it works.
You input a UNC path.The code creates a map with a temporary letter,gets the information of that drive and then disconnects it.So you dont have to map the drive before you run the program
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.