我最近写了一段这样的代码:
protected void OpenImg_Click(object sender, EventArgs e)
{
int i = 0;
string PathToFolder = "C://LiveSite/img/XL/";
var dirInfo = new DirectoryInfo(PathToFolder);
string FileName = Variables.param + "XL.jpg";
var foundFiles = dirInfo.GetFiles(FileName);
if (foundFiles.Length == 1)
{
ClientScript.RegisterStartupScript(this.GetType(), "openFoundImage", "window.open('" + PathToFolder + foundFiles[i].Name + "');", true);
}
}
}
}
它的路径正确,但它不起作用,因为它正在拉出 c:// 驱动器,所以我将图像的位置改为:http://www.companysite.com/img/XL/
。当我替换string PathToFolder = "C://LiveSite/img/XL/";
为http://www.companysite.com/img/XL/
我收到错误“不支持 URI 格式”
因此,我将代码更改为:
protected void OpenImg_Click(object sender, EventArgs e)
{
int i = 0;
string uriPath = "http://www.companysite.com/img/XL/";
string localPath = new Uri(uriPath).LocalPath;
string FileName = Variables.param + "XL.jpg";
var foundFiles = localPath.GetFiles(FileName); //ERROR
if (foundFiles.Length == 1)
{
ClientScript.RegisterStartupScript(this.GetType(), "openFoundImage", "window.open('" + uriPath + foundFiles[i].Name + "');", true);
}
}
}
}
现在我在使用 .GetFiles 时遇到错误 - 我应该使用什么来代替 GetFiles?我的代码从网络上提取图像是否正确?任何帮助,将不胜感激。