Downloading files in VB.NET

Status
Not open for further replies.

Person

Baseband Member
Messages
82
I just thought this could be useful, downloading files using the System.Net namespace :).

First of all you have to import the System.Net namespace
Code:
Imports System.Net
Then you need to declare a new instance of a WebClient class which will be used to download the data.
Code:
Dim webc As New WebClient
Now there are two ways you can download, download to a file, or download the data as bytes.
THis is how you would download a file and as bytes:
Code:
Dim data() As Bytes = webc.DownloadData("http://www.techist.com")
This will download the HTML of the main page of the forums. But to the HTML in lets say a textbox you need to translate to a string like this:
Code:
Dim stringdata As String = System.Text.Encoding.Ascii.GetString(data)
Then you can assign it to lets say a textbox to display the html.

Other way is to download things to a file:
Code:
webc.DownloadFile("http://www.techist.com", "C:\techforumshtml.html")
First argument is the address of the file and then the path at which to save it.

I hope this is useful to someone :D.
 
And the C# version
just thought this could be useful, downloading files using the System.Net namespace .

First of all you have to import the System.Net namespace

Code:
using System.Net;

Then you need to declare a new instance of a WebClient class which will be used to download the data.

Code:
WebClient webc = new WebClient();

Now there are two ways you can download, download to a file, or download the data as bytes.
THis is how you would download a file and as bytes:

Code:
byte[] data = webc.DownloadData("http://www.techist.com");

This will download the HTML of the main page of the forums. But to the HTML in lets say a textbox you need to translate to a string like this:

Code:
string stringdata = System.Text.Encoding.Ascii.GetString(data);

Then you can assign it to lets say a textbox to display the html.

Other way is to download things to a file:

Code:
webc.DownloadFile("http://www.techist.com", "C:\techforumshtml.html");

First argument is the address of the file and then the path at which to save it.

I hope this is useful to someone .
 
Status
Not open for further replies.
Back
Top Bottom