Newsgroup: it.comp.lang.visual-basic
Titolo: EMERGENZA: Capacità e Spazio disponibile

Ciao, sono in possesso del VisualBasic 5.0, e per la creazione di un
programma mi servono alcune righe di codice che calcolino la capacità e lo
spazio disponibile di un drive selezionato durante la fase di esecuzione.
Se qualcuno ha la soluzione al mio problema la scriva al mio indirizzo 
E-Mail: 
dico...@mail.ncn.it
 
Un grazie anticipato a tutti coloro che si sono interessati alla mia
richiesta.

 spazio disponibile di un drive selezionato durante la fase di esecuzione.
Ecco i due esempi che Microsoft fornisce. Il primo ed il secondo sono per
Visual Basic 4 e si differenziano per il fatto che il primo calcola lo
spazio libero del drive corrente mentre il secondo di un drive qualunque;il
terzo esempio e' invece per VB in generale. Non ho ancora nulla su VB5, ma
questi tre articoli dovrebbero essere sufficienti.
PRIMO
How to Get Current Drive's Free Disk Space in VB 4.0
Article ID: Q147686 
Creation Date: 28-FEB-1996
Revision Date: 04-MAR-1996 
The information in this article applies to: 
•Professional and Enterprise Editions of Microsoft Visual Basic, 16-bit and
32-bit, for Windows, version 4.0 
SUMMARY 
This article shows how to find out how much free disk space is available on
the current drive by calling the DiskSpaceFree function found in
STKIT416.DLL for Win16 or STKIT432.DLL for Win32. Both dynamic-link
libraries (DLLs) ship with the Setup kit in Microsoft Visual Basic version
4.0 for Windows. The DiskSpaceFree function is useful for finding the free
space available on a drive. If you use the DiskSpaceFree function in your
Visual Basic program, you need to distribute STKIT416.DLL or STKIT432.DLL
to your customers. 
MORE INFORMATION 
There is also a function in the Win32 API (Application Programmers
Interface) called GetDiskFreeSpace provided by the Win32 operating system
that retrieves information about a specified disk, including the amount of
free space on the disk. Refer to Method II in the example below. There are
also other functions in the STKIT416.DLL or STKIT432.DLL that may be
useful. You can use the AllocUnit function to get the disk allocation unit
for the current drive, and use the SetTime function to set the destination
file's date and time to the source file's date and time. 
Examples of using these SETUPKIT functions are also included in the
\VB\SETUPKIT\SETUP1\SETUP1.VBP project which is part of the SetupWizard. 
Steps to Finding Out How Much Free Space Is Available on Drive C 
1.Start a new project in Visual Basic. Form1 is created by default. 2.Add
two Label controls (Label1 and Label2) and one Command button (Command1) to
Form1. 3.Using the following table as a guide, set the properties of the
controls you added in step 2. 
Control Name Property New Value 
   Command1       Caption        Press for Free Space on Drive C
   Label1         AutoSize       True
   Label2         AutoSize       True
•Place the following code in the general declarations section of
   Form1:
   #If Win32 Then
   Private Declare Function DiskSpaceFree Lib "STKIT432.DLL" Alias _
   "DISKSPACEFREE" () As Long
   Private Declare Function GetDiskFreeSpace Lib "kernel32" Alias _
   "GetDiskFreeSpaceA" ( ByVal lpRootPathName As String, _
   lpSectorsPerCluster As Long, lpBytesPerSector As Long, _
   lpNumberOfFreeClusters As Long, lpTotalNumberOfClusters As Long) _
   As Long
   #Else
   Private Declare Function DiskSpaceFree Lib "STKIT416.DLL" () As Long
   #End If
•Place the following code in the Command1 Click event procedure of Form1:
   Private Sub Command1_Click()
   Dim free_Space As Long
   ChDrive "C:"
   ' Method I (using the Win16 API).
   free_Space = DiskSpaceFree()
   Label1.Caption = "(Method I: ) The total free space on Drive C: = " _
   & Str$(free_Space) & " bytes"
   ' Method II (using the Win32 API).
   #If Win32 Then
   Dim numSectorsPerCluster As Long
   Dim numBytesPerSector As Long
   Dim numFreeClusters As Long
   Dim numTotalClusters As Long
   Dim success As Boolean
   success = GetDiskFreeSpace("C:\", numSectorsPerCluster, _
   numBytesPerSector,  numFreeClusters, numTotalClusters)
   free_Space = numSectorsPerCluster * numBytesPerSector * _
   numFreeClusters
   Label2.Caption = _
   "(Method II:) The total free space on Drive C: = " & _
   Str$(free_Space) & " bytes"
   #Else
   Label2.Caption = "Use Method I for Win16 applications"
   #End If
   End Sub
•From the Run menu, click Start (ALT, R, S), or press the F5 key to run
   the program. Click the Command1 button to view the free disk space on
   drive C.
SECONDO
How To Find and View the Amount of Free Disk Space on a Drive
Article ID: Q153091 
Creation Date: 28-JUN-1996
Revision Date: 01-JUL-1996 
The information in this article applies to: 
•Professional and Enterprise Editions of Microsoft Visual Basic, 32-bit
only, for Windows, version 4.0 
SUMMARY 
From Visual Basic, it is possible to use the Win32 API function
GetDiskFreeSpace to find and view the amount of free disk space on a
specified drive. The code sample below shows this in practice. 
MORE INFORMATION 
Sample Code 
1.Start a new project, Form1 is created by default. 2.From the File menu,
choose Remove File. 3.From the Insert menu, choose Module. 4.Add the
following code to the Module1 code window: 
Declare Function GetDiskFreeSpace Lib "kernel32" Alias _
"GetDiskFreeSpaceA" (ByVal lpRootPathName As String, _ lpSectorsPerCluster
As Long, lpBytesPerSector As Long _ _lpNumberOfFreeClusters As Long,
lpTotalNumberOfClusters As Long) _ As Long 
Public Type DiskInformation 
       lpSectorsPerCluster As Long
       lpBytesPerSector As Long
       lpNumberOfFreeClusters As Long
       lpTotalNumberOfClusters As Long
End Type 
Sub main() 
       Dim info As DiskInformation
       Dim lAnswer As Long
       Dim lpRootPathName As String
       Dim lpSectorsPerCluster As Long
       Dim lpBytesPerSector As Long
       Dim lpNumberOfFreeClusters As Long
       Dim lpTotalNumberOfClusters As Long
       Dim lBytesPerCluster As Long
       Dim lNumFreeBytes As Double
       Dim sString As String
       lpRootPathName = "c:\"
       lAnswer = GetDiskFreeSpace(lpRootPathName, lpSectorsPerCluster, _
       lpBytesPerSector, lpNumberOfFreeClusters, lpTotalNumberOfClusters)
       lBytesPerCluster = lpSectorsPerCluster * lpBytesPerSector
       lNumFreeBytes = lBytesPerCluster * lpNumberOfFreeClusters
       sString = "Number of Free Bytes : " & lNumFreeBytes & vbCr & vbLf
       sString = sString & "Number of Free Kilobytes: " & (lNumFreeBytes _
          / 1024) & "K" & vbCr & vbLf
       sString = sString & "Number of Free Megabytes: " & _
          Format(((lNumFreeBytes / 1024) / 1024), "0.00") & "MB"
       MsgBox sString
End Sub 5.Press the F5 key or choose Start from the Run menu to start the
program. You will see a message box displaying the free disk space on drive
C:. 
TERZO
Tip 200: Determining the Amount of Free Disk Space
December 5, 1995
Abstract
This article explains how to retrieve the amount of available disk space
from within a Microsoft® Visual Basic® application.
Using the GetDiskFreeSpace Function
When writing information to disk from within a Microsoft® Visual Basic®
application, you may need to determine whether there is enough space
available on the disk drive before starting to write the new data to it.
The Microsoft Windows® application programming interface (API)
GetDiskFreeSpace function allows you to calculate how much free space there
is on a specified disk drive. To use this function, include the following
Declare statement in the General Declarations section of your application:
Declare Function GetDiskFreeSpace Lib "kernel32" Alias "GetDiskFreeSpaceA" 
   (ByVal lpRootPathName As String, lpSectorsPerCluster As Long, 
   lpBytesPerSector As Long, lpNumberOfFreeClusters As Long, 
   lpTotalNumberOfClusters As Long) As Long
The GetDiskFreeSpace function requires five arguments, as follows:
lpRootPathName
A string containing the root directory for the disk drive for which you
want to retrieve information. If NULL, the default directory's root path is
used.
LpSectorsPerCluster
A long value that will contain the number of sectors per cluster.
LpBytesPerSector
A long value that will contain the number of bytes per sector.
LpNumberOfFreeClusters
A long value that will contain the number of free clusters on the disk.
LpTotalNumberOfClusters
A long value that will contain the number of clusters on the disk.
After executing the GetDiskFreeSpace function, either a value of True is
returned if the function was successful or a value of False if the function
was not successful.
To calculate the total number of bytes available on the disk, you need to
multiply the number of bytes per sector by the number of sectors per
cluster. Then, multiply this result by the number of free clusters on the
disk. This total gives you the number of bytes of free space on the disk.
Example Program
This program shows how to retrieve the amount of free disk space.
1.Create a new project in Visual Basic. Form1 is created by default.2.Add a
Text Box control to Form1. Text1 is created by default. Set its MultiLine
property to True.3.Add a Command Button control to Form1. Command1 is
created by default.4.Add the following code to the Click event for
Command1: 
Private Sub Command1_Click()
    Dim X As Long
    X = GetDiskSpace("c:\")
    If X Then
        sFreeSpace = Format$(CurrentDisk.FreeBytes, "###,###,##0")
        sTotalSpace = Format$(CurrentDisk.TotalBytes, "###,###,##0")
        sFreePcnt = Format$(CurrentDisk.FreePcnt, "Percent")
        sUsedPcnt = Format$(CurrentDisk.UsedPcnt, "Percent")
    End If
    text1.Text = "Free Space: " & sFreeSpace & " Percent: " 
       & sFreePcnt & Chr(13) & Chr(10)
    text1.Text = text1.Text & "Total Bytes free: " & sTotalSpace & 
       " Percent: " & sUsedPcnt
End Sub
5.Create a new function called GetDiskSpace. Add the following code to this
function (note that the "X =" line must be typed as a single line of code):
Function GetDiskSpace(sRootPathName As String) As Long
    Dim X As Long
    Dim lSectorsPerCluster As Long, lBytesPerSector As Long
    Dim lNumberOfFreeClusters As Long, lTotalNumberOfClusters As Long
 
    X = GetDiskFreeSpace(sRootPathName, lSectorsPerCluster,
lBytesPerSector, 
       lNumberOfFreeClusters, lTotalNumberOfClusters)
    GetDiskSpace = X
 
    If X Then
        CurrentDisk.RootPath = sRootPathName
        CurrentDisk.FreeBytes = lBytesPerSector * lSectorsPerCluster * 
           lNumberOfFreeClusters
        CurrentDisk.TotalBytes = lBytesPerSector * lSectorsPerCluster * 
           lTotalNumberOfClusters
        CurrentDisk.FreePcnt = (CurrentDisk.TotalBytes -
CurrentDisk.FreeBytes) 
           / CurrentDisk.TotalBytes
        CurrentDisk.UsedPcnt = CurrentDisk.FreeBytes /
CurrentDisk.TotalBytes
    Else
        CurrentDisk.RootPath = ""
        CurrentDisk.FreeBytes = 0
        CurrentDisk.TotalBytes = 0
        CurrentDisk.FreePcnt = 0
        CurrentDisk.UsedPcnt = 0
        Exit Function
    End If
End Function
6.From the Visual Basic Insert menu, select Module to create a new module.
Module1.Bas is created by default.7.Add the following Type, Constant, and
Declare statements to Module1.Bas (note that the Declare statement must be
typed as a single line of code): 
Declare Function GetDiskFreeSpace Lib "kernel32" Alias "GetDiskFreeSpaceA" 
   (ByVal lpRootPathName As String, lpSectorsPerCluster As Long, 
   lpBytesPerSector As Long, lpNumberOfFreeClusters As Long, 
   lpTotalNumberOfClusters As Long) As Long
Type DISKSPACEINFO
    RootPath As String * 3
    FreeBytes As Long
    TotalBytes As Long
    FreePcnt As Single
    UsedPcnt As Single
End Type
Global CurrentDisk As DISKSPACEINFO
Run the example program by pressing f5. Click the Command Button control.
The program displays the number and percentage of free bytes of disk space,
and the total number and percentage of bytes used.