【问题标题】:The given path's format is not supported using File.ReadAllText使用 File.ReadAllText 不支持给定路径的格式
【发布时间】:2013-11-25 11:45:04
【问题描述】:

我有一个 WPF 应用程序。我正在从 XML 文件加载一些数据。

我收到一个错误:

System.NotSupportedException was unhandled
Message: The given path's format is not supported.

在这一行:

string html = File.ReadAllText(Advertisement.DescriptionUrl);

xml中的url是:

http://mysitetest.com/x/x/Assets/shop/shopdetails/Coffee/image.png

有什么解决办法吗?

【问题讨论】:

  • 请大家在投反对票时添加评论,谢谢
  • 如果我们让 File.ReadAllText 不处理 url 的事实,image.png 几乎不会是一个文本文件,所以将它作为文本读取可能不是你想要的。

标签: c# .net xml wpf


【解决方案1】:

File.ReadAllText 用于在文件系统上获取 文件名 - 而不是 URL。

您需要使用WebClient.DownloadString 之类的方式获取它:

string text;
using (WebClient client = new WebClient())
{
    text = client.DownloadString(url);
}
// Now use text

【讨论】:

    【解决方案2】:

    这是一个网址,而不是文件路径。

    使用WebClient 对象请求资源:

    string html;
    using (WebClient client = new WebClient()) {
      html = client.DownloadString(Advertisement.DescriptionUrl);
    }
    

    【讨论】:

      猜你喜欢
      • 2011-11-13
      • 2012-04-21
      • 1970-01-01
      • 2017-09-14
      • 2014-12-08
      • 1970-01-01
      • 1970-01-01
      • 2012-05-15
      相关资源
      最近更新 更多