- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
描述
使用 webclient 的 DownloadFileAsync 下载多个文件,并利用文本文件输入 URL 进行下载。
问题
我使用的方法根本不会下载文件。只是运行,什么也不做。它填充列表数组然后退出程序而不下载单个文件。我在谷歌上搜索了解决方案,但人手不足。然后尝试在此处的数据库中搜索具有相同结果的解决方案。感谢您的帮助。
问题
代码
下载Class.cs
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(主类)
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();
}
}
}
最佳答案
除了糟糕的设计之外,还有很多问题会导致您的代码无法(或无法正常工作)。
只要您不了解多线程,我就建议您使用同步方法来避免所有这些问题。
关于c# - 使用 webclient DownloadFileAsync 多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5048154/
我正在使用 C# 中的 WebClient 下载文件。我正在使用 client.DownloadFileAsnyc()。在过去这工作得很好,任何异常都会被捕获并在完成处理程序中返回 (AsyncCom
我正在制作一个 WPF 应用程序。我使用 WebClient 的 DownloadFileAsync 下载文件。我一次从文件夹中下载每个文件。我第一次调用 DownloadProtocol 它工作正常
我正在尝试编写一个简单的应用程序,它可以从网络上下载一个文件。 class Program { static void Main(string[] args) { We
我需要使用 WebClient 下载文件在 PowerShell 2.0 中,我想显示下载进度,所以我这样做了: $activity = "Downloading" $client = New-Obj
WebClient DownloadFileAsync() 不适用于相同的 URl 和凭据... 有什么线索吗? static void Main(string[] args) {
我正在尝试使用 WPF 和 MVVM 从我的网络服务器下载一个大文件 (500 mb)。因此,以下属性都绑定(bind)到某种控件(进度条)。问题是,应用程序仍然挂起,即使在使用 DownloadFi
DownloadFileAsync 和 DownloadFileTaskAsync 有什么区别? 什么时候应该使用一个而不是另一个?任何例子将不胜感激。 最佳答案 一般模式 - 如果您发现两个名称以
我正在尝试通过以下方式在我的服务器上下载一个 zip 文件 WebClient.DownloadFileAsync(new Uri(DownloadLink),
正如您在代码中间看到的那样,有一个丑陋的线程阻塞代码等待文件下载并在命令行中启用进度报告。我的意思是,它仍然比 Thread.Sleep() 或忙等待要好,对吧?不管怎样,我知道 Wait/Pulse
这几乎就是标题中的全部问题。我有一个 WPF C# Windows 应用程序,我为用户下载文件,现在想显示速度。 最佳答案 mWebClient.DownloadProgressChanged +=
美好的一天。我正在使用 DownloadFileAsync 处理文件下载器类。在正常情况下,一切正常。但是当我下载文件并禁用网络连接时,下载进度会无限期停止,不会引发任何错误或调用任何回调。任何想法如
我正在使用下面的代码从 TFS 服务器下载多个附件: foreach (Attachment a in wi.Attachments) { WebClient wc = new WebC
我正在使用 WebClient 的 DownloadFileAsync 方法从服务器下载一些文件,在我对代码的非正式测试中我不禁注意到在 VS2010 中,它在启动时阻塞了大约 3 秒,在我看来,这首
所以我认为 Webclient.DownloadFileAysnc 会有一个默认超时,但是查看文档我无法在任何地方找到任何关于它的信息,所以我猜它没有。 我正在尝试像这样从 Internet 下载文件
描述 使用 webclient 的 DownloadFileAsync 下载多个文件,并利用文本文件输入 URL 进行下载。 问题 我使用的方法根本不会下载文件。只是运行,什么也不做。它填充列表数组然
安全取消 DownloadFileAsync 操作的最佳方法是什么? 我有一个线程(后台 worker )开始下载并管理它的其他方面,当我看到线程有 CancellationPending == tr
所以我正在使用 webClient.DownloadFileAsync(url, sFilePathToWriteFileTo); 下载文件 网址是string url = "http://local
所以基本上我的下载文件是: public void DownloadFile() { settings_btn.Enabled = false; label1.Text = "Chec
我正在使用 WebClient 按以下方式从 Internet 下载一些文件: try { ManualResetEvent mr = new ManualResetEvent(false);
我有此 C# 代码,但最终的 esi.zip 结果为 0 长度或基本上为空。该 URL 确实存在并确认手动下载文件。我很困惑买这个。 string zipPath = @"C:\download\es
我是一名优秀的程序员,十分优秀!