Know VB file system object FSO

zhaozj2021-02-17  49

In VB programming, you often need to deal with the file system, such as getting the remaining space of the hard disk, determining if the folder or file exists. Before the VB introduced file system object (File System Object), complete these features require calling Windows API functions or uses some complicated processes to achieve complex programming, bad and reliable. Document system objects provided by Windows, everything is much simpler. The following author raised some commonly used examples in programming, provided to everyone in the form of functions or processes, readers can use them directly in programming, or improve more powerful functions. To apply an FSO object, you must reference a type library named Scripting, the method is to execute the VB6.0 menu item "Project / Reference", add the "Microsoft Scripting Runtime" in the reference list box. Then we can see many objects and their methods under the Scripting type library in the Object Browser.

1. Judgment the drive letter of the CD-ROM: Function getcdrom () 'Returns the drive of the CD-ROM DIM FSO AS New FileSystemObject' Creating an instance of the FSO object DIM FSODRIVE AS DRIVE, FSODRIVES AS DRIVES 'Defining Drive, Drive Collection Object SET FSODRIVES = Fso.Drives for Each FsoDrive in FsoDrives' through all the available drive If FsoDrive.DriveType = CDRom Then 'if the type of drive is CDrom GetCDROM = FsoDrive.DriveLetter' outputs the letter Else GetCDROM = "" End If Next Set Fso = Nothing Set FsoDrive = Nothing Set FsoDrives = Nothing End Function 2, whether a file, folder exists: 'returns a Boolean value: True presence, False does not exist, filername file name Function FileExist (filename As String) Dim Fso As New FileSystemObject if Fso .FileExists (filename) = True Then FileExist = True Else FileExist = False End If Set Fso = Nothing End Function 'returns a Boolean value: True presence, False does not exist, foldername folder Function FolderExist (foldername As String) Dim Fso As New FileSystemObject If fso.folderexists (foldername) = True1 FolderExist = true else folderexist = false endiff = nothing end function 3, get drive parameters: 'Return to disk total Space size (unit: m), drive = disk letter A, C, D ... function allspace (drive as string) DIM FSO AS New FileSystemObject, DRV As Drive Set Drv = fso.getdrive (drive) 'Get DRV object Example if drv.isready the 'If the drive exists (there is a disk in the floppy drive or the optical drive, the hard disk access is normal) AllSpace = Format (DRV. TOLSIZE / (2 ^ 20), "0.00")' Talking bytes Else Allspace = 0 end if set fso = nothing set drv = nothing end function 'Return Disk available space Size (unit: m), Drive = Panker A, C, D ... Function FreeSpace (Drive) DIM FSO AS New FileSystemObject , DRV AS Drive Set DRV =

Fso.getdrive (drive) if drv.isready life free = format (drv.freespace / (2 ^ 20), "0.00") end if set fso = Nothing set drv = nothing end function 'Get drive file system type, drive = Dr. C, D ... Function FSTYPE (DRIVE AS STRING) DIM FSO AS New FileSystemObject, DRV As Drive Set Drv = fso.getdrive (drive) if drv.isready the fSTYPE = DRV.FILESYSTEM ELSE FSTYPE = "" End If Set Fso = Nothing Set Drv = Nothing End Function 4, folder path acquisition system: 'return to the Windows folder path Function GetWindir () Dim Fso As New FileSystemObject GetWindir = Fso.GetSpecialFolder (WindowsFolder) Set Fso = Nothing End Function' returns the Windows / System folder path Function GetWinSysdir () Dim Fso As New FileSystemObject GetWinSysdir = Fso.GetSpecialFolder (SystemFolder) Set Fso = Nothing End Function 5, the integrated use of: a generic file backup process: 'filename = filename, drive = drive , Folder = folder (1 floor) Sub Backupfile (filename as string, drive as string, folder as string) DIM FSO AS New FileSystemObject 'Creating an FSO Object Instance DIM DEST_PATH A s String, Counter as long counter = 0 do while counter <6 'If the drive is not ready, continue to detect.

转载请注明原文地址:https://www.9cbs.com/read-28917.html

New Post(0)