我正在使用 File.OpenRead 方法读取文件,我给出了这个路径
http://localhost:10001/MyFiles/folder/abc.png
我也试过,但是没有成功
http://localhost:10001//MyFiles//abc.png
但它的给予
URL Formats are not supported
当我像这样给出我的驱动器的物理路径时,它工作正常 d:\MyFolder\MyProject\MyFiles\folder\abc.png
如何为 Http 路径提供文件路径?
这是我的代码
public FileStream GetFile(string filename)
{
FileStream file = File.OpenRead(filename);
return file;
}
最佳答案
看看 WebClient
( MSDN docs ),它有很多从网络下载数据的实用方法。
如果您希望资源作为流
,请尝试:
using(WebClient webClient = new WebClient())
{
using(Stream stream = webClient.OpenRead(uriString))
{
using( StreamReader sr = new StreamReader(stream) )
{
Console.WriteLine(sr.ReadToEnd());
}
}
}
关于c# - 不支持 URL 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17918433/