用c#如何下载EXE,DLL,ZIP等类型的文件?PS:最好能有例子~

解决方案 »

  1.   

    下载1. C#: 
    /// <summary>
    /// 文件下载
    /// </summary>
    /// <param name="FullFileName"></param>
    private void FileDownload(string FullFileName)
    {
    FileInfo DownloadFile = new FileInfo(FullFileName); 
    Response.Clear();
    Response.ClearHeaders();
    Response.Buffer=false;
    Response.ContentType="application/octet-stream";
    Response.AppendHeader("Content-Disposition","attachment;filename=" +HttpUtility.UrlEncode(DownloadFile.FullName,System.Text.Encoding.UTF8));
    Response.AppendHeader("Content-Length",DownloadFile.Length.ToString());
    Response.WriteFile(DownloadFile.FullName);
    Response.Flush();
    Response.End();
    }
    2. vb.net
    Public Sub WriteDLWindow(ByVal strFileName As String, ByVal page As System.Web.UI.Page)
            Try
               If File.Exists(page.MapPath(strFileName)) Then
                   Dim TargetFile As FileInfo = New FileInfo(page.MapPath(strFileName))
                   '清除缓冲区流中的所有内容输出.
                   page.Response.Clear()
                   '向输出流添加HTTP头 [指定下载/保存 对话框的文件名]
                   page.Response.AppendHeader("Content-Disposition", "attachment; filename=" + page.Server.UrlEncode(TargetFile.Name))

    '繁体格式
                    'page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8))                '向输出流添加HTTP头 [指定文件的长度,这样下载文件就会显示正确的进度
                    page.Response.AppendHeader("Content-Length", TargetFile.Length.ToString())
                    '表明输出的HTTP为流[stream],因此客户端只能下载.
                    page.Response.ContentType = "application/octet-stream"
                    '发送文件流到客户端.
                    page.Response.WriteFile(TargetFile.FullName)
                    '停止执行当前页
                    page.Response.End()
                End If
            Catch ex As Exception
                Throw ex
            End Try
        End Sub
      

  2.   

    设置保存文件格式(example: *.xls):
    Response.ContentType = "application/ms-excel";
      

  3.   

    这个好象是ASP的下载。
    有没有WinForm的下载方法?
      

  4.   

    直接链接到文件的URL,打开不就下载了?为什么要这么复杂?
      

  5.   

    同意zjmotion() ( )只要实体在硬盘上,而数据库中保护了各个实体文件的路径,只要输出这个路径就行了,不是吗???
      

  6.   

    1.用WebRequest发送请求2.得到数据流:WebResponse.GetResponse().GetResponseStream()3.将主体写入文件中
      

  7.   

    winform 看这个System.Net.WebClient
      

  8.   

    我对C#.NET不是很熟悉,刚刚接触,不知道具体有哪些控件可用。
    如果是ASP.NET,那么就简单了,直接Response.Redirect("http://文件的路径")就可以下载。
    如果是WinForm程序,那就找个可以引用地址的控件,WebBrower?这个可以,只要
    axWebBrowser1.Navigate("http://文件路径")
    就可以开始下载,当然,仅仅为了引用一个地址而动用WebBrower好像有欠妥当,但我真的不知道有没有其他控件可以实现,实在想不到就这样用着吧。呵
      

  9.   

    能不能给个WinForm下载.exe,.dll,.zip的例子啊~
    谢谢!~
      

  10.   

    自己改一下: private void DownLoadFile()
            {
                try
                {
                    WebClient client = new WebClient();                if (!Directory.Exists(Application.StartupPath + @"\update"))
                    {
                        Directory.CreateDirectory(Application.StartupPath + @"\update");
                    }
                    for (int i = 0; i <= this.listView1.Items.Count - 1; i++)
                    {
                        string fileName = this.listView1.Items[i].SubItems[0].Text;
                        string url = @"http://172.16.120.155/update/UpdateFile/" + fileName;
                        string file = Application.StartupPath + "\\update\\" + fileName;
                        this.label2.Text = "正在下载:" + fileName;                    Stream myStream = client.OpenRead(url);
                        byte[] bBuffer = new byte[10240];
                        int nRealCount = 0;
                        DateTime dtStart=DateTime.Now ;
                        TimeSpan ts;
                        int lenth = Convert.ToInt32(this.listView1.Items[i].SubItems[1].Text.ToString());
                        int cur = 0;
                        progressBar1.Value = 0;
                        FileStream fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write);
                        //do
                        //{                       
                        //    dtStart = DateTime.Now;
                        //    nRealCount = myStream.Read(bBuffer, 0, bBuffer.Length);
                        //    if (nRealCount > 0)
                        //    {
                        //        fs.Write(bBuffer, 0, nRealCount);
                        //    }
                        //    ts = DateTime.Now - dtStart;
                        //    cur = cur + nRealCount;
                        //    double pe = cur;
                        //    pe = pe * 100 / lenth;
                        //    int va = Convert.ToInt32(pe);
                        //    progressBar1.Value = va;                      
                        //    this.label2.Text = "正在下载:" + fileName + "   " + cur / 1024 + " KB/" + lenth / 1024 + " KB";
                        //    MessageBox.Show("" + ts.Milliseconds + "  " + ts.TotalMilliseconds);
                        //    // this.label2.Text = "正在下载:" + fileName + "   " + cur / 1024 + " KB/" + lenth / 1024 + " KB"+"  下载速度:"+((double )nRealCount /(int)(ts.TotalMilliseconds/1000)) +" kb/s";
                        //    // 计算流量可以用 nRealCount / ts.Seconds来进行估算
                        //}
                        int value=0;
                        int counter = 1;
                        do
                        {
                            if (counter % 10 == 0)
                            {
                                ts = DateTime.Now - dtStart;                            int perSecondsValue = (int)((double)value / (ts.TotalMilliseconds / 1000)) / 1024;                                                        this.label3.Text = "速度:" +perSecondsValue +" k/s";
                                dtStart = DateTime.Now;
                                value = 0;
                            }                      
                           
                            nRealCount = myStream.Read(bBuffer, 0, bBuffer.Length);
                            if (nRealCount > 0)
                            {
                                fs.Write(bBuffer, 0, nRealCount);
                            }
                            cur = cur + nRealCount;
                            double pe = cur;
                            pe = pe * 100 / lenth;
                            int va = Convert.ToInt32(pe);
                            progressBar1.Value = va;
                            
                            this.label2.Text = "正在下载:" + fileName + "   " + cur / 1024 + " KB/" + lenth / 1024 + " KB";                                  
                            
                            value += nRealCount;
                            counter++;
                        }
                        while (nRealCount > 0);
                        fs.Close();                    
                        // client.DownloadFile(url, file);
                        this.listView1.Items[i].SubItems.Add("已下载");
                    }                this.progressBar1.Visible = false;
                    this.label3.Visible = false;
                    this.label2.Text = "正在更新文件,请稍后...";
                    string[] fileDir = Directory.GetFileSystemEntries(Application.StartupPath + @"\update");                foreach (string str in fileDir)
                    {
                        int pos = str.LastIndexOf(@"\");
                        string FileName = str.Substring(pos);
                        string FilePath = Application.StartupPath + FileName;
                        File.Copy(str, FilePath, true);
                    }                this.label2.Text = "程序更新已完成!";                this.label1.Text = this.finish;
                    this.btnNext.Enabled = false;
                    this.listView1.Visible = false;
                    this.btnExit.Text = "完成(&F)";
                    step++;
                }
                catch (Exception errMsg)
                {
                    MessageBox.Show(errMsg.Message);
                }
            }
      

  11.   

    qyfjl(Try to improve my skills.) 能不能吧你发的那个完整的例子给我啊~
    谢谢~
      

  12.   

    如果不要进度条。(new WebClient()).DownloadFile("http://123.com/love.zip", "c:\love.zip");就可以下载loave.zip。同理可以下载别的东西。不过,需要进度条的话可以在CodeProject上面找到很多。
      

  13.   

    那能不能讲讲ListView在这里起到的功能。比如int lenth = Convert.ToInt32(this.listView1.Items[i].SubItems[1].Text.ToString()); 什么意思?
      

  14.   

    楼主做的是b/s还是c/s
    要是c/s我这里倒是有一个读写文件的类