【问题标题】:How To Download HTML File Name As A String?如何将 HTML 文件名下载为字符串?
【发布时间】:2014-09-12 03:16:34
【问题描述】:
Uri uri = new Uri("http://rotter.net/scoopscache.html");
client.Encoding = System.Text.Encoding.GetEncoding(1255);
//page = client.DownloadString("http://rotter.net/scoopscache.html");
page = client.DownloadFileAsync(uri, @"myhtml.html");
listsext.Ext(page);

如果我使用的是 DownloadString,它会将 html 内容分配给页面。 我希望页面包含文件名,所以我尝试使用 DownloadFileAsync,但在这一行出现错误:

Error   2   Cannot implicitly convert type 'void' to 'string'

【问题讨论】:

  • 嗯,那是因为 DownloadFileAsync 返回 void,而不是字符串。 “异步”并不意味着它会给你文件名,它意味着它会异步地完成工作。您希望它包含什么文件名? scoopscache.html?
  • 是的 scoopscache.html 或我将提供的任何其他文件名,但是是下载的文件名。
  • 您似乎知道它是什么文件名,而无需实际下载它。你为什么不能把它从这条线上拉出来:Uri uri = new Uri("http://rotter.net/scoopscache.html");

标签: c# .net


【解决方案1】:

错误说明:

WebClient.DownloadFileAsync

public void DownloadFileAsync(
    Uri address,
    string fileName
)

它不返回任何东西(void),你将它传递给listsext!当然你会得到一个Exception

无法将类型“void”隐式转换为“string”

你想要达到的目标:(我的猜测,提供的信息不多)

你想拥有文件名,你不需要下载它。

Uri u = new Uri("http://rotter.net/scoopscache.html")
filename = Path.GetFileName(u.LocalPath);

请看这里:Get file name from URI string in C#

【讨论】:

  • 这个方法有an overload,只需要2个参数。
  • @mason:谢谢,我错过了它的重载版本。更新了答案。
猜你喜欢
  • 2011-01-06
  • 1970-01-01
  • 2015-04-18
  • 2012-02-28
  • 2013-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多