VB: 写文件时怎样判断磁盘已满

建一窗体,加入一Label1 Caption属性为 "将文本框中的文件拷到A:盘"
在其下加一文本框 text1 Text属性为 "c:/command.com"
再下面加一Label2 Caption 属性为空
再下面加一Command1 Caption属性为 "拷贝"
加入以下代码


Option Explicit


Private Declare Function GetDiskFreeSpace Lib "kernel32" Alias "GetDiskFreeSpaceA" (ByVal lpRootPathName As String, lpSectorsPerCluster As Long, lpBytesPerSector As Long, lpNumberOfFreeClusters As Long, lpTotalNumberOfClusters) As Long

Private Type DiskInformation
    lpSectorsPerCluster As Long
    lpBytesPerSector As Long
    lpNumberOfFreeClusters As Long
    lpTotalNumberOfClusters As Long
End Type
    
Private Sub Command1_Click()
On Error GoTo errHandler
Dim fso As Object
Dim temp As String
Dim fsoFile As Object
Dim fileSize As Long
    Text1.Enabled = False
    If Trim(Text1.Text) = "" Then
        MsgBox "输入文件名!", vbInformation, "提示"
    Else
        Set fso = CreateObject("Scripting.FileSystemObject")
        If fso.FileExists(Text1.Text) Then
            Set fsoFile = fso.GetFile(Text1.Text)
            fileSize = fsoFile.Size
        Else
            MsgBox "文件不存在!", vbInformation, "提示"
            Exit Sub
        End If
    End If
    Label2.Caption = "检查软盘驱动器..."
    Label2.Refresh
    temp = Dir("a:/")
    Label2.Caption = "检查软盘容量..."
    If fileSize < GetSize Then
        Label2.Caption = "正在拷贝文件..."
        Label2.Refresh
        fso.CopyFile Text1.Text, "a:/"
    Else
        MsgBox "软盘空间不足!", vbInformation, "提示"
        Label2.Caption = "错误!"
        Exit Sub
    End If
    Label2.Caption = "拷贝成功!"
    Text1.Enabled = True
    Exit Sub
errHandler:
    Label2.Caption = "错误!"
    Text1.Enabled = True
    MsgBox "请插入软盘!", vbInformation, "提示"
End Sub
    
Private Function GetSize() As Long
    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 = "a:/"
    lAnswer = GetDiskFreeSpace(lpRootPathName, lpSectorsPerCluster, lpBytesPerSector, lpNumberOfFreeClusters, lpTotalNumberOfClusters)
    lBytesPerCluster = lpSectorsPerCluster * lpBytesPerSector
    lNumFreeBytes = lBytesPerCluster * lpNumberOfFreeClusters
    GetSize = lNumFreeBytes
End Function

'Form1
VERSION 5.00
Begin VB.Form Form1
   Caption         =   "Form1"
   ClientHeight    =   2160
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   3945
   LinkTopic       =   "Form1"
   ScaleHeight     =   2160
   ScaleWidth      =   3945
   StartUpPosition =   3  '窗口缺省
   Begin VB.TextBox Text1
      Height          =   315
      Left            =   255
      TabIndex        =   1
      Text            =   "c:/command.com"
      Top             =   675
      Width           =   3390
   End
   Begin VB.CommandButton Command1
      Caption         =   "拷贝"
      Height          =   495
      Left            =   1290
      TabIndex        =   0
      Top             =   1530
      Width           =   1215
   End
   Begin VB.Label Label2
      Height          =   255
      Left            =   240
      TabIndex        =   3
      Top             =   1095
      Width           =   3435
   End
   Begin VB.Label Label1
      Caption         =   "将文本框中的文件拷到A:盘"
      Height          =   255
      Left            =   165
      TabIndex        =   2
      Top             =   195
      Width           =   3225
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private Declare Function GetDiskFreeSpace Lib "kernel32" Alias "GetDiskFreeSpaceA" (ByVal lpRootPathName As String, lpSectorsPerCluster As Long, lpBytesPerSector As Long, lpNumberOfFreeClusters As Long, lpTotalNumberOfClusters) As Long

Private Type DiskInformation
    lpSectorsPerCluster As Long
    lpBytesPerSector As Long
    lpNumberOfFreeClusters As Long
    lpTotalNumberOfClusters As Long
End Type
    
Private Sub Command1_Click()
On Error GoTo errHandler
Dim fso As Object
Dim temp As String
Dim fsoFile As Object
Dim fileSize As Long
    Text1.Enabled = False
    If Trim(Text1.Text) = "" Then
        MsgBox "输入文件名!", vbInformation, "提示"
    Else
        Set fso = CreateObject("Scripting.FileSystemObject")
        If fso.FileExists(Text1.Text) Then
            Set fsoFile = fso.GetFile(Text1.Text)
            fileSize = fsoFile.Size
        Else
            MsgBox "文件不存在!", vbInformation, "提示"
            Exit Sub
        End If
    End If
    Label2.Caption = "检查软盘驱动器..."
    Label2.Refresh
    temp = Dir("a:/")
    Label2.Caption = "检查软盘容量..."
    If fileSize < GetSize Then
        Label2.Caption = "正在拷贝文件..."
        Label2.Refresh
        fso.CopyFile Text1.Text, "a:/"
    Else
        MsgBox "软盘空间不足!", vbInformation, "提示"
        Label2.Caption = "错误!"
        Exit Sub
    End If
    Label2.Caption = "拷贝成功!"
    Text1.Enabled = True
    Exit Sub
errHandler:
    Label2.Caption = "错误!"
    Text1.Enabled = True
    MsgBox "请插入软盘!", vbInformation, "提示"
End Sub

'获得A盘可用空间大小
Private Function GetSize() As Long
    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 = "a:/"
    lAnswer = GetDiskFreeSpace(lpRootPathName, lpSectorsPerCluster, lpBytesPerSector, lpNumberOfFreeClusters, lpTotalNumberOfClusters)
    lBytesPerCluster = lpSectorsPerCluster * lpBytesPerSector
    lNumFreeBytes = lBytesPerCluster * lpNumberOfFreeClusters
    GetSize = lNumFreeBytes
End Function

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值