Application of FSO Object Model in VB

xiaoxiao2021-03-06  14

From the first edition of Visual Basic, the process of related files in VB is implemented by using Open, WRITE, and other related statements and functions. With the continuous development of software technology, plus the maturity of the programming concept of objects, these file operation statements have not adapted to the increasing complexity of the software. Therefore, starting from VB6.0, Microsoft puts forward a new file system object FSO.

First, introduction

The full name of the file system object FSO is File System Object, which proposes a method that is different from traditional file operation statements to process files and folders. By using Object.Method, the action of a series of operation files and folders is directly implemented by calling the object itself by calling the property itself.

The FSO object model can not only create, change, move, and delete files as using traditional file operation statements, but also detect if there is a specified folder, if exist, then this folder is on what location on the disk. More joily, the FSO object model can also get information about files and folders, such as name, creation date, or recently modified date, etc., as well as information of the drive used in the current system, such as the type of drive is CD-ROM or Move disks, how much is the remaining space of the current disk. And before you want to get this information must be implemented by calling the corresponding functions in the Windows API function.

The FSO object model is included in the Scripting Type Library (Scrrun.dll), which contains five objects of Drive, Folder, File, FileSystemObject, and TextStream. Where DRIVE is used to collect the drive information, such as the type of disk space or drive; Folder is used to create, delete, or move folders, and can perform operations such as path to the system query folder; File's basic operation and Folder basically Different from the Files operation is mainly for files on the disk; FileSystemObject is the most important object in the FSO object model, which provides a complete set of complete sets of, delete files, folders, collecting drives, folders , Method of file-related information. It should be noted that the method provided by the FSO object model is redundant, that is, in actual use, the different methods of different objects included in the FSO object model are the same operation, and the method of the FileSystemObject object is directly active. In the rest of the object, it is not necessary to mention FileSystemObject objects in the following article. Don't mention that it is not mentioned, in fact, the FileSystemObject object is everywhere in the entire FSO object model; the last TextStream object is used To complete the reading and writing of the file.

After a preliminary understanding of the FSO object model, the following we will further explain different objects through the actual code.

Second, the application of FSO object model

(1) Creating an FSO object model

Since the FSO object is included in the Scripting Type Library (Scrrun.dll), you first need to reference this file in the project before use, click Project, "Reference", then select "Microsoft" in the "Reference dialog" The check box before scripting runtime, and then click OK.

To create an FSO object, two methods can be used, one is to declare a variable as an FSO object type:

Dim fsotest as new filesystemObject;

The other is to create an FSO object by the CreateObject method:

SET FSOTEST = CREATEOBJECT ("scripting.filesystemObject");

Which declaration method is used in actual use, it can be determined according to the personal usage habit. After the creation of the FSO object model is completed, you can access the properties of each object using the method of the created object model to obtain the desired information or perform the relevant operation, the specific method is described below to describe the respective objects.

(2) DRIVE object

The DRIVE object has been mentioned above is used to get information about each drive in the current system. Since there is no way for the Drive object, its application is expressed by attribute, so we must be familiar with the properties of the Drive object:

Property Features AvailableSpace Returns the space capacity available to the user on the specified drive or network share. Driveletter returns a letter specified a local drive or network drive, which is read-only. DriveType returns the type of disk that specifies the drive. FileSystem returns the file system type used by the specified drive. FreeSpace returns the disk space available on the specified drive or shared drive, this property is read-only. IsReady determines if the specified drive is ready. PATH Returns the path to the specified file, folder, or drive. Rootfolder returns a Folder object that represents a root folder of a specified drive. Read-only properties. SerialNumber returns a decimal serial number for uniquely identifying the disk volume. ShareName Returns the network shared name TOTALSIZE to the specified drive to return to the total space size of the drive or network share in bytes. Volumename Set or returns the volume label name of the specified drive.

From the above attribute, you can see that the DRIVE object has substantially all drive information required for daily operations, so it is very convenient in use. The following is described with an instance to describe the use of Drive objects. First, create a project in VB, then add a command button, set its CAPTION to "TestDrive", then add the following code in the Click event:

Dim fsoTest As New FileSystemObject Dim drv1 As Drive, sReturn As StringSet drv1 = fsoTest.GetDrive ( "C: /") sReturn = "Drive" & "C: /" & vbCrLfsReturn = sReturn & "VolumeName" & drv1.VolumeName & vbCrLfsReturn = sReturn & "Total Space:" & FormatNumber (drv1.TotalSize / 1024, 0) sReturn = sReturn & "Kb" & vbCrLfsReturn = sReturn & "Free Space:" & FormatNumber (drv1.FreeSpace / 1024, 0) sReturn = sReturn & "KB" & vbrlfsreturn = SRETURN & "FileSystem:" & drv1.filesystem & vbcrlfmsgbox Sreteturn

Where the GetDrive method returns a Drive object corresponding to the drive in the specified path. The syntax format of this method is Object.getDrive DRIVESPEC, OBJECT is the name of an FSO object, and DriveSpec is used to specify the name of the drive. Press F5 to run the above code, press the TestDrive button to pop up a message box to display the information of the C drive.

(3) Folder objects In the FSO object model, the FSO object model provides a wealth of methods related to folder operations. These methods are: FileSystemObject objects. Method for folder CreateFolder Create a folder deletefolder Delete a folder MoveFolder Mobile a folder CopyFolder Copy a folder FOLDEREXISTS Find a folder to get an instance getParentFoldernAme for the existing Folder object on the drive to find a folder's parent folder to find the path to the system folder.

Folder object method use Delete Create a folder Move Move a folder Copy Copy a folder name Name Retrieve the name of the folder

It is necessary to emphasize a little more, before we mentioned that the method included by the FSO object model is redundant, so the DELETE, MOVE, COPY method of the Folder object, and the deleteFolder, MoveFolder, and CopyFolder methods of the FileSystemObject object are actually the same, so One of them can be optionally used in actual use.

Like the Drive object, the application of the Folder object is demonstrated below. Add a new project under VB, then add three command buttons above, then add the following code in the general part of Form1:

Option Explicit Dim Fsotest AS New FileSystemObject Dim Folder1 As Folder Enter the following code in the Click event of three command buttons: Private subdcreate_click () 'Get the Folder object. Set folder1 = fsotest.getFolder ("c:") 'Create a folder Fsotest.createFolder ("C: / Test") MsgBox "Folder C: / Test Has create" End SubPrivate Sub Cmddelete_Click () Get the Drive object. Set folder1 = fsotest.getfolder ("c:") 'Delete FSOTEST.DELETEFOLDER ("C: / Test") MSGBox "Folder C: / Test Has Deleted" End SubPrivate Sub CmdgetPro_Click () Gets the information about folders Dim sReturn As String Set folder1 = fsoTest.GetFolder ( "C: / Windows") 'sReturn = "the folder's Attributes is" & folder1.Attributes & vbCrLf' acquisition time of the last visit of sReturn = sReturn & "the folder's last access time is "& folder1.DateLastAccessed & vbCrLf 'get the last modified time sReturn = sReturn &" the folder's last modify time is "& folder1.DateLastModified & vbCrLf' to get the size sReturn folder = sReturn &" the folder's size is "& FormatNumber (Folder1.Size / 1024, 0) SRETURN = SRETURN & "KB" & VBCRLF 'Judgment file or folder type SRETURN = SRETURN & "TYPE IS" & folder1.type & vbrlf msgbox Sretend Sub above the code mentioned in the above code The grammatical form of the CreateFolder method is Object.createFolder (FolderName). FolderName Specifies the name of the folder to be created, and the grammatical form of the DeleteFolder method is Object.Deletefolder folderspec [, force], where folderspec is used to specify the name of the folder to be deleted, force is an optional Boolean parameter If you want to delete the read-only property, set the value to true, default is false. As for the properties of the Folder objects used in the code, they are not detailed, and the reader can refer to the related content in the VB document.

(4) File object and textstream object

This part of this segment is no longer repeated due to the replication, deletion, movement of the File object and the Folder object. Here mainly tells the use of File objects and TextStream objects to operate text files. Usually the operation of the text file includes creation data in a text file, adding data to a text file, deleting data for text files. These operations can be done through the File object and the FileSystemObject object. However, before use, first create a text file, which can be done through three methods. One method is the CreateTextFile method using the FILESystemObject object. To create an empty text file, you can use the following statement:

Dim Fsotest As New FileSystemObject, Fil1 AS FileSet Fil 1 = fsotest.createtextFile ("c: /testfile.txt", true)

The second method is the OpenTextFile method set with the FileSystemObject object with the ForWriting flag.

Dim Fsotest As New FileSystemObject, Ts1 As New TextStreamset Ts1 = fsotest.OpenTextFile ("c: /testfile.txt", forwriting

The third method is the OpenAsTextStream method set with the File object with the ForWriting flag:

DIM FSOTEST AS '=============================================================================================================================================================================================================== # file. The first parameter of the OpenTextFile method is to specify the file name of the open; the second parameter is that the specified file is read, added, or written; the third parameter determines whether the new file is to be established when the specified file name does not exist. The format of the fourth parameter specified file is ASCII or Unicode. This method returns a TextStream object. The following code segment reads the contents of a text file into a TextBox control. The Readall method in the TextStream is used to read the contents of the file, and then use the Close method to close the file. Dim objFSO As FileSystemObject Dim objText As TextStream Set objFSO = New FileSystemObject Set objText = objFSO.OpenTextFile (App.Path & "/temp.txt", _ ForReading, False, TristateUseDefault) Text1.Text = objText.ReadAll () Call objText. CLOSE In order to be able to write content in a text file, you can open the file first, then enter the required value using the Write method in the TextStream. Set objtext = objfso.opentextfile (app.path & "/temp.txt", _ forwriting, false, tristateusedefault) call objtext.write (Text1.text) call objtext.close

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

New Post(0)