winform下载网页代码
1:webClient client=new WebClient();
client.Downloadstring(地址)
client.Downloadfile(地址,保存路径)
2:后台线程dowork
3:声明委托
//江县城写道主界面(控件)
private void Meg()
{
messageBox.Show("下载成功")!
}
Action action=new Action(Meg);//这就是委托,就是将方法给
this.invoke()
一下是个具体事例:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.backgroundWorker1.RunWorkerAsync();
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("各种Info快来下载吧!");
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 3010; i < 3050; i++)
{
WebClient client = new WebClient();
try
{
client.DownloadFile(@"http://job.cnblogs.com/offer/" + i + "/", @"d:\" + i + ".html");
}
catch { }
}
//Action是委托
Action action = new Action(msg);
this.Invoke(action);
}
private void msg()
{
MessageBox.Show("下载完成!");
}
}