首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用webclient的DownloadFileAsync多个文件

使用webclient的DownloadFileAsync多个文件
EN

Stack Overflow用户
提问于 2011-02-19 01:00:31
回答 1查看 19K关注 0票数 3

描述

使用webclient的DownloadFileAsync下载多个文件,并利用文本文件作为URL输入供下载。

问题

我所使用的方法根本不会下载文件。只是跑了什么也没做。它填充列表数组,然后退出程序,而不下载一个文件。我在谷歌上搜索过解决方案,但发现人手不足。然后尝试在数据库中搜索具有相同结果的解决方案。任何帮助都是非常感谢的。

问题

  1. 为什么这种方法不起作用?
  2. 我能做些什么来改进这一点并从中学习呢。

DownloadClass.cs

代码语言:javascript
复制
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Net;
using System.Threading;
using System.Windows.Forms;

namespace ThreadTest
{
    class DownloadClass
    {
        public struct download
        {
            public static string URL { get; set; }
            public static string file { get; set; }
            public static string[] link;
            public static int downloadcount;
        }

        public static List<string> list = new List<string>();
        public static WebClient wc = new WebClient();

        public static void Download()
        {
            int count = 0;
            download.URL = list[0];
            Uri URI = new Uri(download.URL);
            UriBuilder uri = new UriBuilder(URI);
            download.link = uri.Path.ToLower().Split(new char[] { '/' });

            count = 0;
            // Find file
            foreach (string abs in download.link)
            {
                count++;
                if (abs.ToLower().Contains(".html") || abs.ToLower().Contains(".exe") || abs.ToLower().Contains(".txt"))
                {
                    try
                    {
                        download.file = download.link[count];
                        wc.Proxy = null;
                        wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
                        wc.DownloadFileAsync(URI, Application.StartupPath + "\\" + download.file);
                        break;
                    }
                    catch (Exception)
                    { }
                }
            }
        }

        public static void BeginDownload()
        {
            new Thread(Download).Start();
        }

        public static void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            int count = 0;
            download.downloadcount++;
            download.URL = list[0];
            Uri URI = new Uri(download.URL);
            UriBuilder uri = new UriBuilder(URI);

            download.link = uri.Path.ToLower().Split(new char[] { '/' });

            count = 0;
            // Find file
            foreach (string abs in download.link)
            {
                count++;
                if (abs.ToLower().Contains(".html") || abs.ToLower().Contains(".exe") || abs.ToLower().Contains(".txt"))
                {
                    try
                    {
                        download.file = download.link[count];
                    }
                    catch (Exception)
                    { }
                }
            }
            list.RemoveAt(0);
            if (list.Count > 0)
            {
                wc.DownloadFileAsync(URI, list[download.downloadcount], Application.StartupPath + "\\" + download.file);
            }
            else
            {
                Console.WriteLine("Downloading is done.");
                Environment.Exit(0);
            }
        }
    }
}

Program.cs (主级)

代码语言:javascript
复制
using System;
using System.IO;
using System.Collections.Generic;
using System.Windows.Forms;

namespace ThreadTest
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("Usage: {0} <download txtfile>", Environment.GetCommandLineArgs()[0]);
                Environment.Exit(0);
            }

            int counter = 0;
            string line;
            string format = string.Format("{0}\\{1}", Application.StartupPath, args[0]);

            // Read the file line by line.
            using(StreamReader file = new StreamReader(format))
            {
                while ((line = file.ReadLine())!= null)
                {
                    // Store urls in a list.
                    DownloadClass.list.Add(line);
                    counter++;
                }
            }
            DownloadClass.BeginDownload();
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2011-02-19 01:24:08

除了糟糕的设计之外,还有许多问题导致您的代码不能(或不正确地工作)。

  1. ,您需要确保您的应用程序在下载某些内容时处于生命状态。您当前的应用程序立即退出(您必须等待下载在主应用程序中完成)。
  2. 您的应用程序可以多次下载同一个文件,但根本不下载其他文件(当它们以async=multithreading方式访问静态对象时,您需要完全锁定对象) BTW:从一开始就不要使用静态对象来避免这种情况。
  3. 即使修改了2,仍然可能多次下载同一个文件到同一个文件名中,从而失败。

只要您不了解多线程,我建议您使用同步方法来避免所有这些问题。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5048154

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档