Home > Articles > Programming > Visual Basic

  • Print
  • + Share This

Working with the FileSystemObject

The FileSystemObject is an object that ships with Scripting RunTime library, which comes with Internet Explorer. You can use the FileSytemObject to work with the drives of a computer as well the computer's file system. The FileSystem object is comprehensive, so you might want to read up on it at the following URL: http://msdn.microsoft.com/library/devprods/vs6/vbasic/vbenlr98/vaobjfilesystemobject.htm.

In this program, we use the FileSystemObject to open the cache file that contains the data we requested from the Internet and then read from the file a line at a time. We'll use the FileSystemObject's OpenTextFile method to open the cache file. The OpenTextFile method returns a TextStream object. We can use the TextStream object to read lines of text from the file one at time, adding a Visual Basic carriage return character, vbCrLf, to the end of each line. Then we can concatenate the line onto a string buffer.

The following code in Listing 2 shows the FileSystemObject used within the UserControl_AsyncReadComplete event procedure. Notice that the UserControl_AsyncReadComplete event procedure passes an AsyncProperty object just as the UserControl_AsyncReadProgress event procedure does. Also, notice that we use the PropertyName property of the AsyncProperty to distinguish the download of text data from a download of graphic data.

Listing 2: Data Downloaded from the Internet Is Accessed by Using FileSystemObject to Read from the Data's Cache File

Private Sub UserControl_AsyncReadComplete
(AsyncProp As AsyncProperty) 
'This event if fired when an async transfer is complete
        Select Case AsyncProp.PropertyName 
       'Check the PropertyName property value for the 
       'poem property 
           Case PROP_POEM: 
           'Get the data from the cache file 
           'Use a FileSystemObject to manipulate 
           'the cache file 
           Dim fs As New FileSystemObject 
           'Create a TextStream object that will 
           'contain the text data from the file 
           'that in the Internet cache 
           Dim ts As TextStream 
           Dim str As String 'String buffer 
           'Open a the cache file for the poem and 
           'return a textstream object 
           Set ts = fs.OpenTextFile(mPoemFileName, ForReading) 
           'Traverse the textstream object, line by line 
           'adding the new line onto the string buffer 
           While Not ts.AtEndOfStream 
               'Concatenate the newly read line putting a 
               'line break character at the end of each line 
               str = str & ts.ReadLine & vbCrLf 
       'Display the string buffer, which is the poem, 
       'in the Textbox object. 
       txtPoem.Text = str
    'Let's load the picture 
    Case PROP_PIC: 
            Picture1.Picture = LoadPicture(mPicFileName) 
    Case Else: 
  End Select
End Sub

The last piece of data manipulation that we need to do in the AsyncReadComplete event procedure is to display the graphic data into the custom control's PictureBox control. We display the graphic data by using the Visual Basic LoadPicture() function. LoadPicture() takes a file path and file name of a supported graphics file as an argument. LoadPicture() returns a picture object, which we assign to the Picture property of the PictureBox, Picture1.

That's about all there is to it. Now we need to compile the custom control, package it, and embed it in a Web page for transport to a client machine.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.