Go deep into one of the scripting runtime Libraries

Source: Internet
Author: User

Go deep into one of the scripting runtime Libraries

Www.applevb.com

What is scripting Runtime Library? In general, scripting Runtime Library (hereinafter referred to as Sr) provides
"Forgot" the object of the basic file system operation function in Visual Basic.
Click project | referrences in the menu, and select Microsoft scripting runtime from the references list in the references selection window.
. Click OK to exit and add the scripting Runtime Library to the VB project file. (In the following program
This step must be performed before code is added. This step is used when adding the SR library as mentioned below.) a sr object includes FileSystemObject
Objects and ctionary ary objects correspond to file system operations and dictionary Operations respectively.
For example, dim fsosys as new scripting. FileSystemObject defines a FileSystemObject object.
The FileSystemObject object contains the drive object for obtaining drive information; the file object for copying, deleting, and moving files;
Folder to create, copy, delete, move, and obtain the folder and subfolders folder object; Create, read, and write text files
Textstream object. Next we will introduce the attributes and operation methods of these objects in different categories.
1. Operations on drive objects
A drive object can be used to obtain the capacity, attributes, and other information of the drive defined by the object, and the getdrive side of the FileSystemObject object can be used
To obtain a drive object.
The following is an example of drive object operations.
First, create a new project in VB. Add the SR library. Then add a ListBox control to form1 and a picturebox. Do not change them.
And then add the following code in the form1 code window:
Option explicit

Dim fsosystem as new FileSystemObject
Dim fsodrives as drives
Dim fsodrive as drive

Private sub form_load ()
Dim sdrive as string
Dim sdrivetype as string
Dim bhascdrom as Boolean

Set fsodrives = fsosystem. Drives
For each fsodrive in fsodrives
Sdrive = fsodrive. driveletter &":"
Select case fsodrive. drivetype
Case 0: sdrivetype = "unknown drive"
Case 1: sdrivetype = "Removable Drive"
Case 2: sdrivetype = "fixed drive"
Case 3: sdrivetype = "Remote Drive"
Case 4: sdrivetype = "CDROM Drive"
Case 5: sdrivetype = "RAM disk"
End select

If fsodrive. drivetype = CDROM or fsodrive. drivetype = CDROM then
Bhascdrom = bhascdrom or fsodrive. isready
End if
Sdrive = sdrive & sdrivetype
List1.additem (sdrive)
Next
End sub

Private sub form_unload (cancel as integer)
Set fsosystem = nothing
End sub

Private sub listmediaclick ()
Dim astr $
Dim fsodrive as drive
If list1.listindex>-1 then
Astr = left $ (list1.list (list1.listindex), 1)
Set fsodrive = fsosystem. getdrive (astr)
'Check whether the drive is ready
If not fsodrive. isready then
Msgbox ("the drive is not prepared or the disk is not inserted ")
Exit sub
End if
'Output drive information
With picture1
. CLs
. Currentx = 30:. currenty = 30
Picture1.print "total capacity" + format $ (fsodrive. totalsize ,_
"####################### 0") + "Byte"
Picture1.print "available capacity" + format $ (fsodrive. availablespace ,_
"####################### 0") + "Byte"
Picture1.print fsodrive. driveletter & ": The file system used is :"&_
Fsodrive. filesystem
End
Set fsodrive = nothing
End if
End sub
Run the program to detect all available drives in the system. Then list them on list1 and click the drive list item on list1,
The basic information of the drive is displayed on picture1. If the drive is not prepared (for example, it is not inserted into a disk or a CD), the program will
Prompt. This method can be used to detect whether a disk exists in the soft drive or the CD-ROM drive, and realize the CD Automatic Detection Function in the super solution. Note:
The above program only supports 2 GB of disk capacity when detecting disk capacity, that is, if your partition is larger than 2 GB, the detected capacity
It cannot exceed 2 GB.

2. Operations on folder objects
You can use the getfolder method of FileSystemObject to obtain a folder object. The following example describes how to create a folder object.
And use this object to create, delete, and obtain subfolders.
First, create a project file and add it to the SR library. Add a Treeview control to form1, two commandbutton controls, and then
Add the following code to form1:
Dim fsosys as new scripting. FileSystemObject
Dim fsorootfolder as folder

Private sub form_load ()
Dim fsosubfolder as folder
Dim nodrootnode as node
Dim nodchild as node
Dim astr $

Set nodrootnode = treeview1.nodes. Add (, "root", "C :/")
Set fsorootfolder = fsosys. getfolder ("C :/")
For each fsosubfolder in fsorootfolder. subfolders
Astr = fsosubfolder. Path
Set nodchild = treeview1.nodes. Add ("root", tvwchild, astr, fsosubfolder. Name)
Next

Set fsorootfolder = nothing
Command1.caption = "Creating a directory"
Command2.caption = "delete a directory"
End sub

Private sub form_unload (cancel as integer)
Set fsosys = nothing
End sub

Private sub commandementclick ()
Dim fsofolder as folder

'Check whether the directory exists. If the directory does not exist, create a new directory.
If fsosys. folderexists ("C:/Temp") then
Msgbox ("directory c:/temp already exists, cannot create directory ")
Else
Set fsofolder = fsosys. createfolder ("C:/Temp ")
Set fsofolder = nothing
End if
End sub

Private sub command2_click ()
'Check whether the directory exists. If yes, delete the directory.
If fsosys. folderexists ("C:/Temp") then
Fsosys. deletefolder ("C:/Temp ")
Else
Msgbox ("directory c:/temp does not exist ")
End if
End sub
Run the program. The program creates a folder object pointing to the root directory of the C drive and obtains all its subfolders to add them to the Treeview. Double-click
In treeview1, you can open the branch to view all the sub-directories under the C:/directory. Click command1 to create C:/temp
Directory. If the directory already exists, a prompt is displayed. Click command2 to delete the C:/TEMP directory.

Iii. Operations on file objects
You can use the GetFile method of FileSystemObject to create a file object pointing to a file on the disk. The following example describes how
Obtain the basic information of a file using the file object.
Create a new project file, add the SR library, and add a FileListBox control and a picturebox control to form1.
And then add the following code to form1:
Option explicit

Dim fsosys as new scripting. FileSystemObject

Private sub file1_click ()
Dim fsofile as file
Dim astr $
Dim sdatecreate

On Error goto errfun
'Get the file name in file1 and create a file object accordingly
Set fsofile = fsosys. GetFile ("C:/Windows/" + file1.list (file1.listindex ))

Picture1.cls
Picture1.currenty = 10
Picture1.print "file name" + fsofile. Name
Picture1.print "DOS file name" + fsofile. shortname
Picture1.print "file type" + fsofile. Type
Picture1.print "file size" & STR (fsofile. Size)
Sdatecreate = fsofile. datecreated
Picture1.print "Creation Time" & sdatecreate
Picture1.print "modification time" & fsofile. datelastmodified
Picture1.print "Access time" & fsofile. datelastaccessed
If fsofile. attributes and archive then
Astr = astr + "regular"
End if
If fsofile. attributes and readonly then
Astr = astr + "read-only"
End if
If fsofile. attributes and hidden then
Astr = astr + "hide"
End if
If fsofile. attributes and system then
Astr = astr + "system"
End if
If fsofile. attributes and compressed then
Astr = astr + "compression"
End if

Picture1.print "file type" + astr

Set fsofile = nothing
Exit sub
Errfun:
'If the File Creation Time is unknown, it will cause an error. Here is the error handler segment.
Sdatecreate = "(unknown )"
Resume next
End sub

Private sub form_load ()
File1.path = "C:/Windows"
End sub

Private sub form_unload (cancel as integer)
Set fsosys = nothing
End sub

4. Operations on textstream objects
I/O operations on text files through vbprogramming have always been a headache. If the inout $ function is used to open the file content at one time
If you read all the data into a single string, it is inconvenient to operate on a certain line of strings. At the same time, a string cannot be greater than 65k, resulting in
Cannot read large files. The use of line Input Method loses the convenience of the entire file operation. The textstream object provides
Stream-based file operations make it much easier to operate text files.
You can use the createtextfile method of FileSystemObject or the openastextstream method to create a textstream object.
The object contains all the content in the file. You can open the file in read-only, write-only, and append mode. After a textstream object is created, you can
Operations on textstream directly increase the convenience and security of file operations.
The following is an example of textstream object operations.
First, create a new project file, add the RS library, add three commandbuton controls and a Textbox Control to form1, and create
A help.txt file, and then add the following code to form1:
Option explicit

Dim fsofile as new FileSystemObject
Dim fsotextstream as textstream

Private sub commandementclick ()
If dir $ ("C:/help.txt") = "" then
Msgbox ("C:/help.txt file does not exist, the program will exit ")
Set fsofile = nothing
End
End if
'Open the file
Set fsotextstream = fsofile. opentextfile ("C:/help.txt", forreading)
Text1.text = fsotextstream. readall' read the file to text1
End sub

Private sub command2_click ()
Dim fsotexttemp as textstream

'Close the original file, create a new file with the same name, and write the updated file stream to the new file.
Fsotextstream. Close
Set fsotextstream = nothing
Set fsotexttemp = fsofile. createtextfile ("C:/help.txt", true)
Fsotexttemp. Write text1.text
Fsotexttemp. Close
Set fsotexttemp = nothing

Commandemediclick
Command2.enabled = false
End sub

Private sub command3_click ()
Fsotextstream. Close
Set fsotextstream = nothing
Set fsofile = nothing
End
End sub

Private sub form_load ()
Text1.text = ""
Command1.caption = "open a file"
Command2.caption = "saving files"
Command3.caption = "quit"
Command2.enabled = false
End sub

Private sub text=change ()
Command2.enabled = true
End sub
Run the program. Click command1 to open the C:/help.txt file, press command2 to save the file, and press command3 to exit.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.