Discuz! Board

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1808|回复: 3

[VB] 在VB中使用FSO对象操作文件 [复制链接]

Rank: 8Rank: 8

发表于 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:

From the References dialog box, check Microsoft Scripting Runtime, as shown below, and click OK.

Once 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.FileSystemObject

An 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.FileSystemObject

You 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.




http://www.vb6.us/tutorials/using-fso-file-system-object-vb6

使用道具 举报

Rank: 8Rank: 8

发表于 2012-8-25 18:09:49 |显示全部楼层

FSO Objects

Object

Description

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.

Drive

The 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 object

The File object is used to examine and manipulate files; the Files collection provides a list of files in a folder.

Folder object

The Folder object is used to examine and manipulate folders; the Folders collection provides a list of subfolders in a folder.

TextStream object

Used to read and write text files.

Property of the FileSystemObject

Property

Description

Drives

Returns a Drives collection, which is a list of physical and logical drives on the system.

使用道具 举报

Rank: 8Rank: 8

发表于 2012-8-25 18:11:45 |显示全部楼层

Methods of the FileSystemObject

Method

Description

BuildPath

Appends file path information to an existing file path.

CopyFile

Copies files from one location to another.

CopyFolder

Copies folders and their contents from one location to another.

CreateFolder

Creates a folder.

CreateTextFile

Creates a text file and returns a TextStream object.

DeleteFile

Deletes a file.

DeleteFolder

Deletes a folder and all of its contents.

DriveExists

Determines if a drive exists.

FileExists

Determines if a file exists.

FolderExists

Determines if a folder exists.

GetAbsolutePathName

Returns the full path to a file or folder.

GetBaseName

Returns the base name of a file or folder.

GetDrive

Returns a drive object.

GetDriveName

Returns a drive name.

GetExtensionName

Returns a file extension from a path.

GetFile

Returns a file object.

GetFileName

Returns a filename from a path.

GetFolder

Returns a folder object.

GetParentFolderName

Returns the parent folder name from a path.

GetSpecialFolder

Returns an object pointer to a special folder.

GetTempName

Returns a temporary (randomly generated) file or folder name that can be used with CreateTextFile.

MoveFile

Moves files from one location to another.

MoveFolder

Moves folders and their contents from one location to another.

OpenTextFile

Opens an existing text file and returns a TextStream object.

使用道具 举报

Rank: 8Rank: 8

发表于 2012-8-25 18:15:29 |显示全部楼层

Properties of the Drive Object

Property


Description


AvailableSpace


The amount of available Drive space in bytes.


DriveLetter


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


DriveType


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).


FileSystem


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


FreeSpace


Same as AvailableSpace .


IsReady


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


Path


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


RootFolder


A Folder object containing the root folder of Drive.


SerialNumber


A long value containing the Drive serial number.


ShareName


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


TotalSize


The total Drive size in bytes.


VolumeName


A string value containing the Drive volume name.


Properties of the Folder Object


PropertyDescription
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").

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

Archiver|IC Test Forum Inc.

GMT+8, 2019-3-24 15:33 , Processed in 1.353123 second(s), 12 queries .

Powered by Discuz! X2

© 2001-2011 Comsenz Inc.

回顶部