我想做的是从网页下载文件。文件下载完成后,我将其打印到屏幕上。
using HtmlAgilityPack;
using NAudio.Wave;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication25
{
class Program
{
static void Main(string[] args)
{
Uri remoteUri = new Uri("http://soundcloud.com/dubstep/spag-heddy-the-master-vip/download");
string fileName1 = "t", myStringWebResource = null;
// Create a new WebClient instance.
using (WebClient myWebClient = new WebClient())
{
myStringWebResource = remoteUri + fileName1;
// Download the Web resource and save it into the current filesystem folder.
myWebClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Url1_DownloadStringCompleted);
myWebClient.DownloadFileAsync(remoteUri, fileName1);
}
}
public static void Url1_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
return;
yes();
}
public static void yes()
{
Console.WriteLine("RRRR");
Console.Read();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我遇到问题的地方是myWebClient.DownloadFileAsync(remoteUri, fileName1);我不确定应该在那儿买什么,而不是在那里。我也已验证该方法myWebClient.DownloadFile。
当我对代码进行以下更改时起作用:将输入字符串更改为URI,固定的本地路径,使用正确的事件处理程序并最后进行Console.Read。我将代码缩短了一点,但您明白了:
static void Main(string[] args)
{
using (WebClient myWebClient = new WebClient())
{
myWebClient.DownloadFileCompleted += DownloadCompleted;
myWebClient.DownloadFileAsync(new Uri("http://someUrl"), @"e:\file.mp3");
}
Console.ReadLine();
}
public static void DownloadCompleted(object sender, AsyncCompletedEventArgs e)
{
Console.WriteLine("Success");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9105 次 |
| 最近记录: |