例如:远程文件的地址是:http://www.abc.com/public/abc.xls
现在要实现:在Winform程序中,输入上面的地址,就将abc.xls下载到本地指定文件夹如何实现?

解决方案 »

  1.   

    使用WebClient class
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemnetwebclientclasstopic.asp
    调用DownloadFile就可以了
      

  2.   

    WebClient.DownloadFile string remoteUri = "http://www.contoso.com/library/homepage/images/";
    string fileName = "ms-banner.gif", myStringWebResource = null;
    // Create a new WebClient instance.
    WebClient myWebClient = new WebClient();
    // Concatenate the domain with the Web resource filename.
    myStringWebResource = remoteUri + fileName;
    Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource);
    // Download the Web resource and save it into the current filesystem folder.
    myWebClient.DownloadFile(myStringWebResource,fileName);        
    Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource);
    Console.WriteLine("\nDownloaded file saved in the following file system folder:\n\t" + Application.StartupPath);
      

  3.   

    http://blog.csdn.net/serversql/archive/2006/03/24/637620.aspx
      

  4.   

    我前几天写的 我给你段代码
    using System;
    using System.Net;
    using System.IO;
    using System.Text;
    using System.Web;
    using System.Windows.Forms;
    namespace httpdown
    {
    /// <summary>
    /// httpDownLoad 的摘要说明。
    /// </summary>
    public class HttpDownLoad
    {
    private long fileLength;
    private long downLength;//已经下载文件大小,外面想用就改成公共属性
    private static bool stopDown;
    public HttpDownLoad()
    {
    fileLength=0;
    downLength=0;
    stopDown=false;
    //
    // TODO: 在此处添加构造函数逻辑
    //
    }
    /// <summary>
    /// 文件下载
    /// </summary>
    /// <param name="url">连接</param>
    /// <param name="fileName">本地保存文件名</param>
    /// <param name="progressBar">进度条</param>
    public void httpDownFile(string url,string fileName,System.Windows.Forms.ProgressBar progressBar)
    {
    Label lable=new Label();
    httpDownFile(url,fileName,progressBar,lable);
    lable.Dispose();
    }
    /// <summary>
    /// 文件下载
    /// </summary>
    /// <param name="url">连接</param>
    /// <param name="fileName">本地保存文件名</param>
    /// <param name="progressBar">进度条</param>
    /// <param name="label">返回已经下载的百分比</param>
    public void httpDownFile(string url,string fileName,System.Windows.Forms.ProgressBar progressBar,Label label)
    {
    stopDown=false;
    Stream str=null,fs=null;
    try
    {
    //获取下载文件长度
    fileLength=getDownLength(url);
    downLength=0;
    if(fileLength>0)
    {
    WebClient DownFile=new WebClient();
    str =DownFile.OpenRead(url);
    //判断并建立文件
    if(createFile(fileName))
    {
    byte[] mbyte = new byte[1024]; 
    int readL=str.Read(mbyte,0,1024);
    fs=new FileStream(fileName,FileMode.OpenOrCreate,FileAccess.Write);
    //读取流
    while(readL!=0)
    {
    if(stopDown)
    break;
    downLength+=readL;//已经下载大小
    fs.Write(mbyte,0,readL);//写文件
    readL=str.Read(mbyte,0,1024);//读流
    progressBar.Value=(int)(downLength*100/fileLength);
    label.Text=progressBar.Value.ToString()+"%";
    System.Windows.Forms.Application.DoEvents();
    }
    str.Close();
    fs.Close();
    }
    }
    }
    catch(Exception ex)
    {
    if(str!=null)
    str.Close();
    if(fs!=null)
    fs.Close();
    MessageBox.Show(ex.Message);
    }
    }
    /// <summary>
    /// 文件下载
    /// </summary>
    /// <param name="url">连接</param>
    /// <param name="fileName">本地保存文件名</param>
    public void httpDownFile(string url,string fileName)
    {
    try
    {
    WebClient DownFile=new WebClient();
    DownFile.DownloadFile(url,fileName);
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    }
    /// <summary>
    /// 获取下载文件大小
    /// </summary>
    /// <param name="url">连接</param>
    /// <returns>文件长度</returns>
    private long getDownLength(string url)
    {
    try
    {
    WebRequest wrq=WebRequest.Create(url);
    WebResponse wrp=(WebResponse)wrq.GetResponse();
    wrp.Close();
    return wrp.ContentLength;
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message);
    return 0;
    }
    }
    /// <summary>
    /// 建立文件(文件如已经存在,删除重建)
    /// </summary>
    /// <param name="fileName">文件全名(包括保存目录)</param>
    /// <returns></returns>
    private bool createFile(string fileName)
    {
    try
    {
    if(File.Exists(fileName))
    {
    File.Delete(fileName);
    }
    Stream s=File.Create(fileName);
    s.Close();
    return true;
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message);
    return false;
    }
    }
    public void downClose()
    {
    stopDown=true;
    }
    }
    }
    这是个完整的类 自己看着用吧
      

  5.   

    jiezhi(风满袖) 此人给的代码已经经过[henry3695]验证,证明改代码真实有效,特此证明
      

  6.   

    private static void GetHtmlFile()
    {
    //using System.Net;
    //using System.Windows.Forms; string remoteUri = @"http://www.baidu.com/img/";
    string fileName = "logo.gif", myStringWebResource = null;
    // Create a new WebClient instance.
    WebClient myWebClient = new WebClient();
    // Concatenate the domain with the Web resource filename.
    myStringWebResource = remoteUri + fileName;
    Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource);
    // Download the Web resource and save it into the current filesystem folder.
    myWebClient.DownloadFile(myStringWebResource,fileName);        
    Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource);
    Console.WriteLine("\nDownloaded file saved in the following file system folder:\n\t" + Application.StartupPath);
    Console.ReadLine(); }