我是根据返回文件列表,(包括URL)
foreach循环下载文件。
问题是,怎么在下载完成事件里获取  我这个下载完成的URL或者是其他的一些信息呢?
因为我要将这些信息入库。谢谢。
  private void CompletedBin(object sender, AsyncCompletedEventArgs e)
           
          WebClient WebC =(WebClient)sender;
         string s = WebC.BaseAddress;
                     
        }
这样获取不到信息,
webclientURL下载C#WINFORM

解决方案 »

  1.   

    byte[] bt = new System.Net.WebClient().DownloadData("http://avatar.profile.csdn.net/C/2/1/1_caofeiweiwu.jpg");
    ??
      

  2.   

    压缩之后下载,压缩代码网上有,一般Web程序都是这样下载的 或者到处excel都是 压缩文件
      

  3.   

    这是我原来弄的压缩代码,也是根据修改别人的代码弄出来了,可以运行。 string the_rar;
            string the_Info;
            Process the_Process;
            ProcessStartInfo the_StartInfo;/// <summary>
            /// 压缩文件t
            /// </summary>
            public void CreatZip()
            {
                string srcFile = Server.MapPath("../Excel/Excels/" + Num + ".xlsx");
                string srcFileOut = Server.MapPath("../Excel/Excels/" + Num + ".zip");
                the_rar = "HaoZipC.exe";
                the_Info = "a " + srcFileOut + " " + srcFile;
                the_StartInfo = new ProcessStartInfo();
                the_StartInfo.FileName = the_rar;
                the_StartInfo.Arguments = the_Info;
                the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                the_StartInfo.WorkingDirectory = "C:\\Program Files\\HaoZip\\";
                the_Process = new Process();
                the_Process.StartInfo = the_StartInfo;
                the_Process.Start();
            }
      

  4.   

    你的事件CompletedBin怎么触发的?
      

  5.   

      
    谢谢 。我只是想知道我下载完成的文件URL就成
      

  6.   

    ...你可以这样,下载到某个文件夹之后 你去查看这个文件夹里面有没有多的文件,有多的 就说明下载完了一个,然后继续查看  用for循环或者foreach遍历
      

  7.   

            public bool bindownload()
            {
                bool flag = true;
                foreach (TMSService.TerminalFile file in termfile)
                {                bool f = true;
                    if (File.Exists(@"" + path + "Filehash.xml"))
                    {
                        XmlDocument xmlDoc = new XmlDocument();
                        xmlDoc.Load(@"" + path + "Filehash.xml");
                        XmlNode root = xmlDoc.DocumentElement;
                        XmlNode a = root.SelectSingleNode("Table1");
                        foreach (XmlNode item in a.ChildNodes)
                        {
                            string[] array = item.InnerText.Split(',');
                            if (array[1] == file.hashCode)
                            {                            if (File.Exists(array[0]))
                                {
                                    f = false;
                                    fileinfo.Append(Path.GetFileName(file.fileUrl) + ""+file.fileTypeName+"\n");
                                    labfileInfo.Text = fileinfo.ToString();
                                    break;
                                }
                                else
                                {
                                    flag = DownloadBin();
                                    
                                }                        }                    }
                        if (f)
                        {
                            flag = DownloadBin();
                         
                        }
                    }
                    else
                    {
                        flag = DownloadBin();
                                    }
                }            return flag;        }        public bool DownloadBin()
            {            bool flag = true;
                try
                {
                    string pathdll = pathRS();
                    labfileBin.Text = Path.GetFileName(binName) + "(应用程序文件)  (正在连接 )";
                    string nowtime = System.DateTime.Now.AddMinutes(1).ToString("yyyy-MM-dd HH:m:ss");
                    Directory.CreateDirectory(pathdll + "Download/" + nowtime);
                    string fileName = Path.GetFileName(binName);
                    string destPath = Path.Combine(pathdll + "Download/" + nowtime, fileName);
                    PathDll = destPath.Replace("\\", "/");
                    urlbin = destPath.Replace("\\", "/");
                    DirectoryInfo dd = new DirectoryInfo(pathdll + "Download/" + nowtime);
                    WebClient webClient = new WebClient();
                    webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(CompletedBin);
                    webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChangedBin);
                    webClient.DownloadFileAsync(new Uri(binName), destPath);            }
                catch (Exception)
                {
                    labfileBin.Text = Path.GetFileName(binName) + "(应用程序文件)";
                    labfileBin.ForeColor = Color.Red;
                    flag = false;
                }
                return flag;
            }
      

  8.   

    你可以把下载文件的名字读出来啊,filename属性
      

  9.   


     webClient.DownloadFileAsync(new Uri(binName), destPath);在下载前把BaseAddress的值设好。 webClient.BaseAddress = binName;
     webClient.DownloadFileAsync(new Uri(binName), destPath);
      

  10.   

    ProgressChangedBin 这个方法你怎么写的呢?
      

  11.   

    你的事web页面还是winform?web页面想实现这个进度条估计是比较难实现的,如果是winform你注意下使用代理,不然界面肯定是会卡住或者报错的。
      

  12.   

    恕我不才,你的代码我看的有点乱,做多文件同时下载,我觉得用到多线程还是比较方便的,要不很难做到每个文件都同时下载并且个字显示各自的进度,而且,如果是同步的话,更如果你把下载的代码放到UI线程中,就更别想更好的效率更好的控制进度条了,界面的重绘除非等到你的所有代码执行完成才会变化,当然这个问题可以解决,但是效率可能不好。至于你想要在下载完成后详细信息入库,我不知道你说的详细信息都包括哪些。文件信息的内容?那可以使用byte[] bt = new System.Net.WebClient().DownloadData("http://avatar.profile.csdn.net/C/2/1/1_caofeiweiwu.jpg");来下载,然后库中的字段类型为二进制,就可以存进去了,最简单的一个方法,不要再完成事件中获取,直接在DownloadData方法后获取不就成了吗?一般webClient.DownloadFileAsync方法执行后,东西也就下来了,除非发生异常。