Windows Develop Bookmark and Share   
 index > Windows Forms General > Download files from web browser
 

Download files from web browser

Hi,

i want to download a file from a web browser. I´m using WebClient class but i´m getting this error: Error11The type or namespace name 'WebClient' does not exist in the namespace 'System.Net' (are you missing an assembly reference?)...

Here´s my code,can you explain me what i'm doing wrong?

And what about to download a folder (not zipped)which contains many files??is that possible?how?

Thanks

using System;

using System.Net;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using Microsoft.WindowsCE.Forms;

using System.IO;

using System.Reflection;

using System.Xml;

using System.Runtime.InteropServices;

using Microsoft.WindowsMobile.Telephony;

using Microsoft.WindowsMobile.DirectX;

.........

namespace MTG_prototipo

{

public partial class LOGIN : Form

{

public LOGIN()

{

InitializeComponent();

}

private void button1_Click_1(object sender, EventArgs e)

{

// string url = "http://server.ubiwhere.com/irt/mtg/";

// System.Diagnostics.Process.Start("IExplore.exe", url);

this.saveFileDialog1.InitialDirectory = @"\";

if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)

{

System.Net.WebClient myClient = new System.Net.WebClient();

myClient.DownloadFile(http://www.xxxxx.xx/xxx.doc, this.saveFileDialog1.FileName);

}

}

}


Vitor Carvalho  Tuesday, June 03, 2008 3:09 PM

The WebClient classis not in the Compact edition of the .NET Framework. Use HttpWebRequest instead:


Code Snippet
String remAdress = "http://www.xxxx.com/data.txt"http://www.xxxx.com/data">http://www.xxxx.com/data.txt"<BR< A>> HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(fremAdress);
httpRequest.Credentials = CredentialCache.DefaultCredentials;

HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
System.IO.Stream dataStream = httpResponse.GetResponseStream();

System.IO.StreamReader streamReader = new System.IO.StreamReader(dataStream);
String remData = streamReader.ReadToEnd();

streamReader.Close();
httpResponse.Close();


To download more files you'll need to know all their names and to download them file by file. or, if the directory where the files are is browsable, i think you could them all:http://www.java2s.com/Code/ASP/Components/ViewfileinfoC.htm (did not test it, see if it works)
Lucian Baciu  Thursday, June 05, 2008 8:54 PM

Hi,

Try to add "System.Net" reference.

Are you writing this application for .NET Compact? And can you write how you have reference in project?

Michal Zalewski  Tuesday, June 03, 2008 4:53 PM

hi,

i'm using "System.Net" actually.

Yes, i'm writing for .NET CF 2.0. I can´t understand what´s wrong...

Vitor Carvalho  Wednesday, June 04, 2008 2:04 PM

The WebClient classis not in the Compact edition of the .NET Framework. Use HttpWebRequest instead:


Code Snippet
String remAdress = "http://www.xxxx.com/data.txt"http://www.xxxx.com/data">http://www.xxxx.com/data.txt"<BR< A>> HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(fremAdress);
httpRequest.Credentials = CredentialCache.DefaultCredentials;

HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
System.IO.Stream dataStream = httpResponse.GetResponseStream();

System.IO.StreamReader streamReader = new System.IO.StreamReader(dataStream);
String remData = streamReader.ReadToEnd();

streamReader.Close();
httpResponse.Close();


To download more files you'll need to know all their names and to download them file by file. or, if the directory where the files are is browsable, i think you could them all:http://www.java2s.com/Code/ASP/Components/ViewfileinfoC.htm (did not test it, see if it works)
Lucian Baciu  Thursday, June 05, 2008 8:54 PM

You can use google to search for other answers

Custom Search

More Threads

• DirectX.AudioVideoPlayback Dispose() method...
• VS2005 crashes with design-time Axhost control
• WinForms, separate data handling and presentation?
• Labels are rendered differently on form designer and application
• Direct Show - Configuring MPEG-2 Demultiplexer Pins
• Visual Studio 2005 Beta 2 released!
• Handling ComboBox Event with AutoCompleteMode set to Suggest and AutoCompleteSource set to AllSystemSources
• need to dock mainmenu with menustrip
• PageSetupDialog problem - PaperSize selection stops working after opening the dialog multiple times and choosing various sizes
• Sql Server Name