Thursday, 15 April 2021

【VISUAL VB.NET】Get DiskFreeSpace

 Imports System

Imports System.Collections.Generic Imports System.ComponentModel Imports System.Data Imports System.Drawing Imports System.Linq Imports System.Text Imports System.Threading.Tasks Imports System.Windows.Forms ' make sure that using System.Diagnostics; is included Imports System.Diagnostics ' make sure that using System.Security.Principal; is included Imports System.Security.Principal ' make sure that using System.Runtime.InteropServices; is included Imports System.Runtime.InteropServices Public Class Form1 Public Sub New() MyBase.New InitializeComponent() comboBox1.SelectedIndex = 2 End Sub #Region "basic function for app" Private Sub lblLink_Click_1(sender As Object, e As EventArgs) Handles lblLink.Click Process.Start("www.vclexamples.com") End Sub Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean If (keyData = Keys.Escape) Then Me.Close() Return True End If Return MyBase.ProcessCmdKey(msg, keyData) End Function #End Region Private Function ConvertBytesToMegabytes(bytes As ULong) As ULong Return (bytes \ 1024) \ 1024 End Function Public Function GetDiskSizeTotal(volumeName As String, ByRef avail As ULong, ByRef total As ULong, ByRef totalfree As ULong) As ULong [MyClass].GetDiskFreeSpaceEx(volumeName, avail, total, totalfree) Return total End Function Public Function GetDiskSize(volumeName As String) As ULong Dim avail As ULong = 0 Dim total As ULong = 0 Dim totalfree As ULong = 0 [MyClass].GetDiskFreeSpaceEx(volumeName, avail, total, totalfree) Return total End Function Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click Dim avail As ULong = 0, total As ULong = 0, totalfree As ULong = 0 ' read size GetDiskSizeTotal(comboBox1.SelectedItem.ToString(), avail, total, totalfree) ' write size in label label1.Text = "Avaliable disk space (for this user): " & ConvertBytesToMegabytes(avail).ToString() & " MB" label2.Text = "Total disk space: " & ConvertBytesToMegabytes(GetDiskSizeTotal(comboBox1.SelectedItem.ToString(), avail, total, totalfree)) & " MB" label3.Text = "Free disk space: " & ConvertBytesToMegabytes(avail).ToString() & " MB" End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load End Sub End Class Public Class [MyClass] ' import from kernel32.dll function GetDiskFreeSpaceEx (total amount) <DllImport("kernel32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _ Friend 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 ' import from kernel32.dll function GetDiskFreeSpaceEx <DllImport("kernel32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _ Friend Shared Function GetDiskFreeSpace(GetDiskFreeSpaces As ULong, ByRef lpSectorsPerCluster As ULong, ByRef lpBytesPerSector As ULong, ByRef lpNumberOfFreeClusters As ULong, ByRef lpTotalNumberOfClusters As ULong) As <MarshalAs(UnmanagedType.Bool)> Boolean End Function End Class

No comments:

Post a Comment

BREAKING: North Carolina automotive group acquires 7 Upstate dealerships

Breaking news from GSA Business Report Click here to view this message in a browser window. ...