none
C#文件下载的问题 RRS feed

  • 问题

  •  

    一朋友写了一段代码用于文件下载,可是每次只能下载到19.9KB就显示完成,文件至少几十MB,不知道何故。

    麻烦达人修改一下。

    代码如下:
    using System.IO;
    using System.Threading;
    using System.Diagnostics;
    using System.Net;
    using System.Net.Sockets;
            private static WebClient client = new WebClient();
            private static StreamWriter writer = null;
            private static Thread thThread;
            private static string url;
            private static string app;
            private static string website;
            private static string filename;

           private static void StartDownload()
            {
                string URL = app;
                int n = URL.LastIndexOf("/");
                string URLAddress = URL.Substring(0, n);
                string fileName = URL.Substring(n + 1, URL.Length - n - 1);
                string Dir = Environment.CurrentDirectory;
                string Path = Dir.ToString() + "\\" + fileName;
                try
                {
                    WebRequest myre = WebRequest.Create(URLAddress);
                }
                catch (WebException exp)
                {
                    Process.Start(@"C:\Program Files\Internet Explorer\iexplore.exe", website);
                }
                try
                {
                    //statusStrip1.Text = "开始下载文件...";
                    client.DownloadFile(URLAddress, fileName);
                    Stream str = client.OpenRead(URLAddress);
                    StreamReader reader = new StreamReader(str);
                    //statusStrip1.Text = "正在接收数据...";
                    FileStream fstr = new FileStream(Path, FileMode.OpenOrCreate, FileAccess.Write);
                    writer = new StreamWriter(fstr);
                    while (reader.Peek() >= 0)
                    {
                        char[] c = new char[10240];
                        reader.Read(c,0, c.Length);
                        writer.Write(c);
                    }
                    writer.Flush();
                    str.Close();
                    fstr.Close();
                    //statusStrip1.Text = "下载完毕!";
                    MessageBox.Show("下载完毕");
                }
                catch (WebException exp)
                {
                    MessageBox.Show(exp.Message, "Error");
                    //statusStrip1.Text = "";
                }

     

    url = "\\JDK6.url";
                app = "http://www.java.net/download/jdk6/6u10/promoted/b28/binaries/jdk-6u10-rc-bin-b28-windows-i586-p-21_jul_2008.exe";
                website = "http://java.sun.com/";
                filename = "JDK6.exe";
                StartDownload();


    共工之臣曰相柳氏,九首,以食于九山。相柳之所抵,厥为泽溪。禹杀相柳,其血腥,不可以树五谷种。禹厥之,三仞三沮,乃以为众帝之台。
    2010年4月26日 3:12

答案

  • byte[] by = new byte[1024];
     int osize = reader.Read(by, 0, (int)by.Length);
     while (osize > 0)
     {
     fstr.Write(by, 0, osize);
     osize = reader.Read(by, 0, (int)by.Length);
     }


    共同努力,共同提高
    kaedei#live.cn My BLOG
    • 已编辑 Kaedei 2010年4月26日 13:10 修正
    • 已标记为答案 KeFang Chen 2010年4月30日 9:10
    2010年4月26日 13:09