请参考:
用Visual C#实现文件下载功能
http://www.pconline.com.cn/pcedu/empolder/gj/vc/10301/126570.html
用Visual C#实现文件下载功能
http://www.pconline.com.cn/pcedu/empolder/gj/vc/10301/126570.html
解决方案 »
- 怎么进行内存读取?
- 使用WebBrowser模拟登陆时,如何去除安全警报对话框
- C#WinForm弹出窗口问题
- C# 算法二叉树 问题,急!谢谢帮助!!!
- 求助一个关于NHibernate的问题
- 我写的例子如何在其它机子上正常运行
- Windows media Palyer
- 在ASP.NET中,怎么才能获得服务器端的一个文件夹里的子文件夹名字啊?(谢谢了,在线)
- ====WEB报表打印:如何使潜入的DLL文件接受参数=====
- sqlDataAdapterEmployees.Fill的问题
- 急:一个文本框如何绑定数据库表的多个字段呢?
- 高手進!!如何在不同的Assembly之間訪問類的靜態方法????
....
WebClient myWebClient = new WebClient();
string configSourcePath = "http://www.csdn.net/images/homeimage/csdn.gif";
myWebClient.DownloadFile(configSourcePath,"c:\\xx.gif");
private void Start_Click(object sender, System.EventArgs e)
{
Thread th = new Thread(new ThreadStart(StartDownload));
th.Start();
}
private void StartDownload()
{
Start.Enabled = false;
string URL = srcAddress.Text;
int n = URL.LastIndexOf('/');
string URLAddress = URL.Substring(0,n);
string fileName = URL.Substring(n+1,URL.Length-n-1);
string Dir = tarAddress.Text;
string Path = Dir+'\\'+fileName;
try
{
WebRequest myre=WebRequest.Create(URLAddress);
}
catch(WebException exp)
{
MessageBox.Show(exp.Message,"Error");
}
try
{
statusBar.Text = "开始下载文件...";
client.DownloadFile(URLAddress,fileName);
Stream str = client.OpenRead(URLAddress);
StreamReader reader = new StreamReader(str);
byte[] mbyte = new byte[100000];
int allmybyte = (int)mbyte.Length;
int startmbyte = 0;
statusBar.Text = "正在接收数据...";
while(allmybyte>0)
{
int m = str.Read(mbyte,startmbyte,allmybyte);
if(m==0)
break;
startmbyte+=m;
allmybyte-=m;
}
FileStream fstr = new FileStream(Path,FileMode.OpenOrCreate,FileAccess.Write);
fstr.Write(mbyte,0,startmbyte);
str.Close();
fstr.Close();
statusBar.Text = "下载完毕!";
}
catch(WebException exp)
{
MessageBox.Show(exp.Message,"Error");
statusBar.Text = "";
}
Start.Enabled = true;
}