Path.Combine 网址出现异常:不支持给定路径的格式

Path.Combine on urls gives exception : The given path's format is not supported

我在以下代码中遇到错误

Uri imagesrc = new Uri("http://somewebsite.com/demo/images/slideshow/29.jpg");
Image image = Image.FromFile(Path.Combine("/comph/", imagesrc.ToString()));

我也试过下面的代码 - 其中 /comph/ 是我的根目录

Image.FromFile(Path.Combine("/comph/","http://some_other_website.com/demo/images/slideshow/29.jpg");

上面的图片 URL 是正确的,当我将这个 URL 粘贴到浏览器时它显示了图片。

以上代码引发异常:

The given path's format is not supported.

这段代码有什么问题?

Path.Combine 不支持 urls.

如果您想使用 Path.Combine

,您必须先将 url 转换为(相对)文件路径

如果你想操纵 urls 你可以使用 the Url constructor that takes a base url and a relative url and combines them.