Compartilhar via


SPBackupRestoreConsole.DiskSize Method

Gets (as out parameters) the total disk space on a volume and the amount of that space that is available to the user.

Namespace:  Microsoft.SharePoint.Administration.Backup
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'Declaration
Public Shared Function DiskSize ( _
    dir As String, _
    <OutAttribute> ByRef freeBytesAvailable As ULong, _
    <OutAttribute> ByRef totalBytes As ULong _
) As Integer
'Usage
Dim dir As String
Dim freeBytesAvailable As ULong
Dim totalBytes As ULong
Dim returnValue As Integer

returnValue = SPBackupRestoreConsole.DiskSize(dir, _
    freeBytesAvailable, totalBytes)
public static int DiskSize(
    string dir,
    out ulong freeBytesAvailable,
    out ulong totalBytes
)

Parameters

  • dir
    Type: System.String

    A path, possibly a UNC path, on the volume whose space information is needed.

  • freeBytesAvailable
    Type: System.UInt64

    When this method returns, contains a DiskSize that represents the total bytes available, on the volume where dir is located, to the user in whose context DiskSize is called. This parameter is passed uninitialized.

  • totalBytes
    Type: System.UInt64

    When this method returns, contains a DiskSize that represents total bytes on the volume where dir is located. This parameter is passed uninitialized.

Return Value

Type: System.Int32
0, if the attempt to report disk size fails; otherwise some other Int32, meaning that the operation succeeded.

Remarks

Typically, dir is the path where you want to store a backup, but any valid path on the same volume can be used and the out parameters will return the same values.

Examples

The following example shows the DiskSize method being used to determine if the backup volume has enough space to hold the components to be backed up. For the full example and a detailed discussion of it, see How to: Programmatically Back Up Content.

private static Boolean EnsureEnoughDiskSpace(String location, Guid backup, SPBackupRestoreObject node)
{
    UInt64 backupSize = SPBackupRestoreConsole.DiskSizeRequired(backup, node);
    UInt64 diskFreeSize = 0;
    UInt64 diskSize = 0;
    Boolean hasEnoughSpace = true;

    try
    {
        SPBackupRestoreConsole.DiskSize(location, out diskFreeSize, out diskSize);
    }
    catch
    {
        diskFreeSize = diskSize = UInt64.MaxValue;
    }

    if (backupSize > diskFreeSize)
    {
        // Report through your UI that there is not enough disk space.
        Console.WriteLine("{0} bytes of space is needed but the disk hosting {1} has only {2}.", backupSize, location, diskFreeSize);
        Console.WriteLine("Please try again with a different backup location or a smaller component.");
        hasEnoughSpace = false;
    }
    else if (backupSize == UInt64.MaxValue || diskFreeSize == 0)
    {
        // Report through your UI that it cannot be determined whether there is enough disk space.
        Console.WriteLine("Cannot determine if that location has enough disk space.");
        Console.WriteLine("Please try again with a different backup location or a smaller component.");
        hasEnoughSpace = false;
    }
    return hasEnoughSpace;

}// end EnsureEnoughDiskSpace
Private Shared Function EnsureEnoughDiskSpace(ByVal location As String, ByVal backup As Guid, ByVal node As SPBackupRestoreObject) As Boolean
    Dim backupSize As UInt64 = SPBackupRestoreConsole.DiskSizeRequired(backup, node)
    Dim diskFreeSize As UInt64 = 0
    Dim diskSize As UInt64 = 0
    Dim hasEnoughSpace As Boolean = True

    Try
        SPBackupRestoreConsole.DiskSize(location, diskFreeSize, diskSize)
    Catch
        diskSize = UInt64.MaxValue
        diskFreeSize = diskSize
    End Try

    If backupSize > diskFreeSize Then
        ' Report through your UI that there is not enough disk space.
        Console.WriteLine("{0} bytes of space is needed but the disk hosting {1} has only {2}.", backupSize, location, diskFreeSize)
        Console.WriteLine("Please try again with a different backup location or a smaller component.")
        hasEnoughSpace = False
    ElseIf backupSize = UInt64.MaxValue OrElse diskFreeSize = 0 Then
        ' Report through your UI that it cannot be determined whether there is enough disk space.
        Console.WriteLine("Cannot determine if that location has enough disk space.")
        Console.WriteLine("Please try again with a different backup location or a smaller component.")
        hasEnoughSpace = False
    End If
    Return hasEnoughSpace

End Function ' end EnsureEnoughDiskSpace

See Also

Reference

SPBackupRestoreConsole Class

SPBackupRestoreConsole Members

Microsoft.SharePoint.Administration.Backup Namespace