一、对于驱动器Drive对象的操作 通过一个Drive对象可以获得该对象定义的驱动器的容量、属性等信息,使用FileSystemObject对象的GetDrive方法可以得到一个Drive对象。 下面是一个Drive对象操作的范例。 首先在VB中建立一个新的工程。加入SR库。然后在Form1中加入一个ListBox控件、一个PictureBox不要改变它们的属性,然后在Form1的代码窗口中加入以下代码: Option Explicit Dim fsoSystem As New FileSystemObject Dim fsoDrives As Drives Dim fsoDrive As Drive Private Sub Form_Load() Dim sDrive As String Dim sDriveType As String Dim bHasCDRom As Boolean Set fsoDrives = fsoSystem.Drives For Each fsoDrive In fsoDrives sDrive = fsoDrive.DriveLetter & “: " Select Case fsoDrive.DriveType Case 0: sDriveType = “未知类型驱动器" Case 1: sDriveType = “可移动驱动器" Case 2: sDriveType =“固定驱动器" Case 3: sDriveType = “远程驱动器" Case 4: sDriveType =“CDROM驱动器" Case 5: sDriveType = “RAM Disk" End Select If fsoDrive.DriveType = CDRom Or fsoDrive.DriveType = CDRom Then bHasCDRom = bHasCDRom Or fsoDrive.IsReady End If sDrive = sDrive & sDriveType List1.AddItem (sDrive) Next End Sub Private Sub Form_Unload(Cancel As Integer) Set fsoSystem = Nothing End Sub Private Sub List1_Click() Dim astr$ Dim fsoDrive As Drive If List1.ListIndex > -1 Then astr = Left$(List1.List(List1.ListIndex), 1) Set fsoDrive = fsoSystem.GetDrive(astr) ’检查驱动器是否准备好 If Not fsoDrive.IsReady Then MsgBox (“该驱动器未准备好或未插入磁盘") Exit Sub End If ’输出驱动器信息 With Picture1 .Cls .CurrentX = 30: .CurrentY = 30 Picture1.Print“总容量" + Format$(fsoDrive.TotalSize, _ “###,###,###,###,###,##0") +“ 字节" Picture1.Print “可用容量" + Format$(fsoDrive.AvailableSpace, _ “###,###,###,###,###,##0") + “字节" Picture1.Print fsoDrive.DriveLetter & “: 使用的文件系统为: " & _ fsoDrive.FileSystem End With Set fsoDrive = Nothing End If End Sub 运行程序,程序检测系统中所有的可用驱动器。然后将它们在List1上列出来,点击List1上的驱动器列表项,该驱动器的基本信息就会显示在Picture1上,如果该驱动器未准备好(例如未插入磁盘或光盘),程序就会出现提示。利用该方法可以检测软驱或者CD-ROM驱动器中是否有盘以及实现向超级解霸中的CD自动检测功能。需要注意的一点是:上面的程序在检测磁盘容量时只支持2GB的容量,也就是说如果你的分区大于2G的话,检测出来的容量也不会超过2GB。 |
正在阅读:使用VB中的SERIPTING RUNTIME LIBRARY使用VB中的SERIPTING RUNTIME LIBRARY
2004-02-14 09:34
出处:eNet学院
责任编辑:zwg
键盘也能翻页,试试“← →”键