[Downloading files in VB.NET] -
Downloading files in VB.NET
Discuss Downloading files in VB.NET
Posted by: Person
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
[/code]
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
[/code]
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.tech-forums.net")
[/code]
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)
[/code]
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.tech-forums.net", "C:\techforumshtml.html")
[/code]
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.
Posted by: Themodem
excelent, just what i was looking for :D
Posted by: Iron_Cross
And the C# version
[quote]
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;
[/code]
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();
[/code]
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.tech-forums.net");
[/code]
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);
[/code]
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.tech-forums.net", "C:\techforumshtml.html");
[/code]
First argument is the address of the file and then the path at which to save it.
I hope this is useful to someone .
[/quote]