测试以查看是否有图像存在于C# [英] Test to see if an image exists in C#

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

问题描述

我写了一个的SiteScope诊断页和一个区域,我们需要测试的是,如果连接到文件/媒体资产是从Web服务器入店。我想我能做到这一点的方法之一是通过加载code中的形象背后,并测试,看看如果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.

我在努力弄清楚如何写这个code。

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

任何帮助是极大的AP preciated。

Any help is greatly appreciated.

感谢

推荐答案

只需使用HEAD。无需下载整幅图像,如果你不需要它。这里是一些样板code。

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天全站免登陆