我需要根据关键字从网上下载大量文件。 我正在执行的步骤是
- 使用 Scraping 找出文件的链接
- 使用 WebClient.DownloadData() 下载 byte[]
- 将 arr 保存到文件。
创建一个线程来下载每个文件以获得更好的性能是否是个好主意。 有什么建议。 谢谢
foreach (string each in arr)
{
Thread t = new Thread(
new ThreadStart(
delegate
{
string[] arr2 = each.Split(new string[] { "http://" }, StringSplitOptions.None);
string[] firstElem = arr2[1].Split(new string[] { " " }, StringSplitOptions.None);
string urlToDownload = @firstElem[0].Replace("\"", string.Empty);
string filName = Path.GetFileName(urlToDownload);
string dirName = DirInAppConfig();
DataRow row;
bool dataExistsInDtKwWithSameDownloadLinkAndFileName;
getRowForKwDownLinkFileName(urlToDownload, filName, out row, out dataExistsInDtKwWithSameDownloadLinkAndFileName);
downloadFile(Client, urlToDownload, dirName, filName, search, row);
}));
t.IsBackground = true;
t.Start();
t.Join();
}
最佳答案
通常服务器限制从一个 IP 下载到 2 个连接。因此,如果所有文件都来自同一台服务器,那么多个线程可能帮不上什么忙。
关于c# - 是否使用线程从网络下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3298059/