Как программно получить размер сетевого диска? - Visual Basic .NET
Формулировка задачи:
Всем доброго времени суток.
Необходимо мониторить размер свободного места на сетевом диске. Есть IO.DriveInfo.TotalFreeSpace но с помощью него можно получить информацию о дисках только на локальном компьютере. А как подключиться к компьютеру в локальной сети (например, имеющему адрес 192.168.x.x)?
Заранее всем спасибо.
Решение задачи: «Как программно получить размер сетевого диска?»
textual
Листинг программы
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("kernel32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function GetDiskFreeSpaceEx(lpDirectoryName As String, ByRef lpFreeBytesAvailable As ULong, ByRef lpTotalNumberOfBytes As ULong, ByRef lpTotalNumberOfFreeBytes As ULong) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
Public Shared Function GetDriveSpace(folderName As String, ByRef freespace As ULong, ByRef totalspace As ULong) As Boolean
Dim free As ULong = 0
Dim total As ULong = 0
Dim dummy2 As ULong = 0
Try
If Not String.IsNullOrEmpty(folderName) Then
If Not folderName.EndsWith("") Then
folderName += ""
End If
If GetDiskFreeSpaceEx(folderName, free, total, dummy2) Then
freespace = free
totalspace = total
Return True
End If
End If
Catch
End Try
Return False
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim totalspace As ULong = 0
Dim freespace As ULong = 0
If GetDriveSpace("\\fileserver\public", freespace, totalspace) Then
'freespace - свободное пространство
' totalspace - всего
TextBox1.Text = Math.Round(freespace / 1024 / 1024, 2) & " Mb"
TextBox2.Text = Math.Round(totalspace / 1024 / 1024, 2) & " Mb"
Else
'
End If
End Sub
End Class