Use file objects in VB

zhaozj2021-02-08  197

Use file objects in VB

The Outline With the release of Visual Basic Scripting Edition 2.0, Microsoft provides a new file operation method - file system object (FileSystemObject). Programming, this is exactly what developers want to use. This object, including some other related objects, encapsulated all file operations. This article will introduce you to these objects and show how to use them to program. The body uses file objects in VB: Yang eyebrows released in Visual Basic to now, most of its main features are most modified. But the file operation part is an exception. In the most original Basic language, the file is written in the file number (File Number). Use a number to operate files, or how much is a little obscured, which makes most beginners feel uncomfortable and difficult to understand. With the release of Visual Basic Scripting Edition 2.0, Microsoft provides a new file operation method - file system object (FileSystemObject). Programming, this is exactly what developers want to use. This object, including some other related objects, encapsulated all file operations. This article will introduce you to these objects and show how to use them to program. To use these objects, you must reference the Microsoft Scripting Runtime (C: /Windows/system/scrun.dll) object library in the VB application. But if you write VBScript for Internet Explore, you can use it directly. Below is an outline of these objects: * Drive - represents a separate drive, can be a floppy drive, a hard disk, or other storage media such as a optical drive. * Drives - A collection of objects, including all drives in the system. * File - represents a disk file representing the system. * Files - Collection object, is a collection of files. Typical is all files represented by a directory. * FILESYSTEMOBJECT - Objects to the entire file system on behalf of the system. Includes all drives, directories, and files. * Folder - represents a directory, which can be local or a remote directory. * Folders - Some collectors of some directories. * TextStream - Text stream object represents an open file used to read and write. Want to get information of all the drives in the system, you can implement the following code.

Sub Main () Dim objFSO As New Scripting.FileSystemObjectDim drvLoop As Scripting.DriveFor ​​Each drvLoop In objFSO.DrivesDebug.Print drvLoop.DriveLetter & ": /" If drvLoop.DriveType = Fixed _Or drvLoop.IsReady ThenDebug.Print "Total size:" _ & Format $ (drvloop.totalsize / (1024 ^ 2), "# 0.00 mb") debug.print "free space:" _ & format $ (drvloop.freespace / (1024 ^ 2), "# 0.00 mb") debug. Print "Volume Label:" & drvloop.voluMenameElelsedebug.print "Disk information unavailable" end ifXext drvloopset Objfso = Nothingend Sub Attempting to access image floppy disk, disc mobile media drive, especially to access the laptop, switchable drive Note: Windows always believes that the drive is existing, regardless of whether there is a disc in it. At this time, you can use the Drive object isReady object to detect whether the drive is ready. Failure to do so may result in the deadlock of the VB application. The Drive object also has some other properties that can be viewed with all its properties with an object browser. In addition to traversing all drives, you can also traverse all files under a folder. This is especially useful when you need to find a specific file throughout the drive. Recursion is Also Very Helpful In There Cases Since You NEED TO Keep Performing The Same Action As you work your way down. The following code is used to count the number of all GIF files. It can also be easily modified into a list of files, and do specify the specific file, and so on.

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

New Post(0)