How to use the My.Computer.Network object to download and upload files in Visual Basic 2005 (914355)



The information in this article applies to:

  • Microsoft Visual Basic 2005

SUMMARY

Learn about how to use the My.Computer.Network object and its methods to download and upload files across a network in Microsoft Visual Basic 2005. This article contains sample steps and sample code that demonstrate how to use the My.Computer.Network object for these tasks.

INTRODUCTION

This article describes how to use the My.Computer.Network object to upload and download files across a network in Visual Basic 2005. By using this object, you can transfer files from a local computer to a remote network resource. Additionally, you can transfer files from a remote network resource to a local computer. To do this, use the following methods in a Visual Basic 2005 application:
  • My.Computer.Network.DownloadFile
  • My.Computer.Network.UploadFile
The My.Computer.Network.UploadFile method sends the specified file from the local computer to the specified remote host address. The My.Computer.Network.DownloadFile method downloads the specified remote file and then saves the file in the specified location on the local computer.

MORE INFORMATION

The following sample steps and sample code demonstrate how to use the My.Computer.Network object to download and upload files in Visual Basic 2005. For more information about these methods, such as information about exceptions that may be thrown and about parameter options, see the "References" section.

Note Before you follow these examples, make sure that the following conditions are true:
  • The local computer can connect to a remote network resource.
  • The folder path exists.
  • You have write permissions for the folder.
Or, you can create a shared network folder on the local computer. Then, use this shared network folder to simulate a remote network resource.

Use the My.Computer.Network.DownloadFile method to download a file

This step-by-step example illustrates how to download a file from a remote network resource and then save the file to a local computer by using the My.Computer.Network.DownloadFile method. To do this, follow these steps:
  1. Start Microsoft Visual Studio 2005.
  2. On the File menu, click New Project.
  3. Under Project Types, click Visual Basic. Under Templates, click Windows Application, and then click OK. By default, a form that is named Form1 is created.
  4. Add a Button control to the Form1 form.
  5. In Design view for the Form1 form, double-click the Button1 control. The Code window appears.
  6. Add the following code example to the Button1_Click event handler. Replace <HostPath> with the host address and file that you want to download. For example, type \\Server\test.txt. Replace <LocalPath> with the path to the folder and file to which you want to save the downloaded file. For example, type C:\Download\test.txt.

    Note If you specify a download folder that does not exist on the local computer, the folder is created automatically.
    Try
        My.Computer.Network.DownloadFile("<HostPath>", "<LocalPath>")
        MessageBox.Show("File downloaded.")
    Catch ex As Exception
        MessageBox.Show("Access failed" & vbCrLf & ex.Message)
    End Try
    
  7. On the Build menu, click Build Project.

    Note Project represents the name of the project.
  8. On the Debug menu, click Start Debugging.
  9. On Form1, click Button1 to download the file.

Use the My.Computer.Network.UploadFile method to upload a file

This step-by-step example illustrates how to upload a file from a local computer to a remote network resource by using the My.Computer.Network.UploadFile method. To do this, follow these steps:
  1. Start Visual Studio 2005.
  2. On the File menu, click New Project.
  3. Under Project Types, click Visual Basic. Under Templates, click Windows Application, and then click OK. By default, a form that is named Form1 is created.
  4. Add a Button control to the Form1 form.
  5. In Design view for the Form1 form, double-click the Button1 control. The Code window appears.
  6. Add the following code example to the Button1_Click event handler. Replace <LocalPath> with the folder and the file that you want to upload. For example, type C:\Upload\test.txt. Replace <LocalPath> with the path of the folder and file to which you want to save the uploaded file. For example, type \\Server\test.txt.

    Note If you upload a file from the local computer that has the same file name as a file that is on the remote computer, the remote file is overwritten.
    Try
        My.Computer.Network.UploadFile("<LocalPath>", "<HostPath>")
        MessageBox.Show("File uploaded.")
    Catch ex As Exception
        MessageBox.Show("Access failed" & vbCrLf & ex.Message)
    End Try
    
    
  7. On the Build menu, click Build Project.

    Note Project represents the name of the project.
  8. On the Debug menu, click Start Debugging.
  9. On Form1, click Button1 to upload the file.

REFERENCES

For more information about the My.Computer.Network object, visit the following Microsoft Developer Network (MSDN) Web site: For more information about the My.Computer.Network.DownloadFile method, visit the following Microsoft Developer Network (MSDN) Web site: For more information about the My.Computer.Network.UploadFile method, visit the following Microsoft Developer Network (MSDN) Web site:

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbWindowsForms kbhowto kbinfo KB914355 kbAudDeveloper