Microsoft KB Archive/914355

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

Article ID: 914355

Article Last Modified on 12/6/2006

-

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 &quot;References&quot; 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:  Start Microsoft Visual Studio 2005. On the File menu, click New Project. 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. Add a Button control to the Form1 form. In Design view for the Form1 form, double-click the Button1 control. The Code window appears.  Add the following code example to the Button1_Click event handler. Replace  with the host address and file that you want to download. For example, type \\Server\test.txt. Replace  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(&quot;<HostPath>&quot;, &quot;<LocalPath>&quot;) MessageBox.Show(&quot;File downloaded.&quot;) Catch ex As Exception MessageBox.Show(&quot;Access failed&quot; & vbCrLf & ex.Message) End Try </li> On the Build menu, click Build .

Note  represents the name of the project.</li> On the Debug menu, click Start Debugging.</li> On Form1, click Button1 to download the file.</li></ol>

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: <ol> Start Visual Studio 2005.</li> On the File menu, click New Project.</li> 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.</li> Add a Button control to the Form1 form.</li> In Design view for the Form1 form, double-click the Button1 control. The Code window appears.</li>  Add the following code example to the Button1_Click event handler. Replace  with the folder and the file that you want to upload. For example, type C:\Upload\test.txt. Replace  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(&quot;<LocalPath>&quot;, &quot;<HostPath>&quot;) MessageBox.Show(&quot;File uploaded.&quot;) Catch ex As Exception MessageBox.Show(&quot;Access failed&quot; & vbCrLf & ex.Message) End Try </li> On the Build menu, click Build .

Note  represents the name of the project.</li> On the Debug menu, click Start Debugging.</li> On Form1, click Button1 to upload the file.</li></ol>

<div class="references_section">