测试 C# 中是否存在图像 [英] Test to see if an image exists in C#

查看:28
本文介绍了测试 C# 中是否存在图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 SiteScope 编写诊断页面,我们需要测试的一个方面是文件/媒体资产的连接是否可以从 Web 服务器访问.我认为可以执行此操作的一种方法是通过隐藏代码加载图像并测试以查看 IIS 状态消息是否为 200.

I am writing a diagnostic page for SiteScope and one area we need to test is if the connection to the file/media assets are accesible from the web server. One way I think I can do this is load the image via code behind and test to see if the IIS status message is 200.

所以基本上我应该能够在站点内导航到这样的文件夹:/media/1/image.jpg 并查看它是否返回 200...如果不抛出异常.

So basically I should be able to navigate to within the site to a folder like this: /media/1/image.jpg and see if it returns 200...if not throw exception.

我正在努力弄清楚如何编写这段代码.

I am struggling to figure out how to write this code.

非常感谢任何帮助.

谢谢

推荐答案

只需使用 HEAD.如果您不需要,则无需下载整个图像.这里有一些样板代码.

Just use HEAD. No need to download the entire image if you don't need it. Here some boilerplate code.

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("url");
request.Method = "HEAD";

bool exists;
try
{
    request.GetResponse();
    exists = true;
}
catch
{
   exists = false;
}

这篇关于测试 C# 中是否存在图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆