arthur 发表于 2012-8-25 18:08:02


本帖最后由 arthur 于 2012-8-25 18:15 编辑

Using the FSO (File System Object) in VB6Level:

The File System Object (FSO) object model provides an object-based tool for working with folders and files. Using "object.method" syntax, it exposes a comprehensive set of properties and methods to perform file system operations such as creating, moving, deleting, and providing information about folders and files. The FSO also provides methods for reading and writing sequential text files, however it does NOT have methods for processing binary or random files.The FSO is (or should be) used primarily with VBScript. VBScript is a scripting language used with ASP for web development; VBScript is also used for Windows scripting. (Windows scripting files, which have a ".vbs" extension, can be thought of as a modern-day replacement for the MS-DOS "BAT" files used in the days of yore). VBScript is a pared-down version of Visual Basic, and as such does not have all of the functionality of "VB proper". One of the things missing in VBScript is the set of native VB file processing statements and functions discussed in the last several topics – so in VBScript, the FSO must be used to manipulate files and folders. However, VB proper can make use of the FSO in addition to its native file processing commands.There are some trade-offs in using the FSO with Visual Basic. On the one hand, the FSO can make certain tasks easier to program with smoother and less arcane syntax than the native VB statements. On the other hand, using the FSO requires adding an additional dependancy to your project, it is slower, it does not support the reading or writing of random and binary files, and it can be disabled by system administrators concerned about security.To use the FSO with your VB project, you must add a reference to "Microsoft Scripting Runtime" (which is the system file "SCRRUN.DLL"). To do this, from the VB IDE, first go to the Project menu, and select References, as shown below: the References dialog box, check Microsoft Scripting Runtime, as shown below, and click OK. you have done the above, you can use the FSO in your VB project. In your code, you must declare an object variable for the FSO and instantiate it. The most concise way of doing this is use the "New" keyword in your declaration, as shown below. (Note: The "Scripting." qualifier is optional here.)Dim objFSO As New Scripting.FileSystemObjectAn alternative way of doing this is to declare the FSO variable without the "New" keyword and instantiate it later with the "Set" statement, as shown below. (Note: Again, the "Scripting." qualifier is optional.)Dim objFSO As Scripting.FileSystemObject...Set objFSO = New Scripting.FileSystemObjectYou can also use "late binding" by declaring the FSO variable as a generic "object" and then using the "CreateObject" method to instantiate it later, as shown below. This is the slowest method, but it is the way it must be done in VBScript. The "Scripting." qualifier is required here.Dim objFSO As Object...Set objFSO = CreateObject("Scripting.FileSystemObject")The tables below show the various objects, properties, and methods available with the FSO.

arthur 发表于 2012-8-25 18:09:49

FSO Objects
FileSystemObject The FSO itself, highest level of the FSO object model. Allows the programmer to interact with Files, Folders and Drives. The programmer can use the FSO objects to create directories, move files, determine whether or not a Drive exists, etc.
DriveThe Drive object is used to examine information on disk, CD-ROM, RAM disk, and network drives; the Drives collection provides a list of physical and logical drives on the system.
File objectThe File object is used to examine and manipulate files; the Files collection provides a list of files in a folder.
Folder objectThe Folder object is used to examine and manipulate folders; the Folders collection provides a list of subfolders in a folder.
TextStream objectUsed to read and write text files.
Property of the FileSystemObject
Drives Returns a Drives collection, which is a list of physical and logical drives on the system.

arthur 发表于 2012-8-25 18:11:45

Methods of the FileSystemObject
BuildPathAppends file path information to an existing file path.
CopyFileCopies files from one location to another.
CopyFolderCopies folders and their contents from one location to another.
CreateFolderCreates a folder.
CreateTextFileCreates a text file and returns a TextStream object.
DeleteFileDeletes a file.
DeleteFolderDeletes a folder and all of its contents.
DriveExistsDetermines if a drive exists.
FileExistsDetermines if a file exists.
FolderExistsDetermines if a folder exists.
GetAbsolutePathNameReturns the full path to a file or folder.
GetBaseNameReturns the base name of a file or folder.
GetDriveReturns a drive object.
GetDriveNameReturns a drive name.
GetExtensionNameReturns a file extension from a path.
GetFileReturns a file object.
GetFileNameReturns a filename from a path.
GetFolderReturns a folder object.
GetParentFolderNameReturns the parent folder name from a path.
GetSpecialFolderReturns an object pointer to a special folder.
GetTempNameReturns a temporary (randomly generated) file or folder name that can be used with CreateTextFile.
MoveFileMoves files from one location to another.
MoveFolderMoves folders and their contents from one location to another.
OpenTextFileOpens an existing text file and returns a TextStream object.

arthur 发表于 2012-8-25 18:15:29

Properties of the Drive Object

The amount of available Drive space in bytes.

A string containing the letter of the Drive without a colon (e.g., "C").

An integer value indicating the Drive type. Possible values are 0 (Unknown), 1 (Removable), 2 (Fixed), 3 (Remote), 4 (CD-ROM) and 5 (RAM Disk).

A string indicating the file system Drive description ("FAT", "FAT32", "NTFS", etc.).

Same as AvailableSpace .

A Boolean indicating whether or not a Drive is ready for use.

A string containing the Drive's path (e.g., "C:\")

A Folder object containing the root folder of Drive.

A long value containing the Drive serial number.

With network drives, returns a string containing the network share name.

The total Drive size in bytes.

A string value containing the Drive volume name.

Properties of the Folder Object

Attributes An integer value indicating Folder's attributes. Possible values are 0 (Normal), 1 (ReadOnly), 3 (Hidden), 4 (System), 8 (Volume), 16 (Directory), 32 (Archive), 64 (Alias), and 128 (Compressed).
DateCreated The date the folder was created.
DateLastAccessed The date the folder was last accessed.
DateLastModified The date the folder was last modified.
Drive The Drive where the folder is located.
IsRootFolder A Boolean indicating whether or not a Folder is the root folder.
Name A string containing the Folder's name.
ParentFolder A string containing the Folder's parent folder name.
Path A string containing the Folder's path.
ShortName A string containing the Folder's name expressed as an MS-DOS compliant ("8.3") short name.
ShortPath A string containing the Folder's path expressed as a short (MS-DOS compliant) path.
Size The total size in bytes of all subfolders and files.
Type A string containing the Folder type (e.g., "File Folder").
页: [1]
查看完整版本: 在VB中使用FSO对象操作文件