检查文件是否存在 [英] Check file exists or not

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

问题描述

当我点击按钮时,页面有一个图像控件和一个按钮



首先检查这个图像是否存在。例如(path是C:/abc/sun.jpeg)它在我们从应用程序调试时起作用。但是当我们部署这个应用程序时,它确实找到了路径.ie C:/abc/sun.jpeg它退出了他们并转到其他地方。

The page having one image control and one button when i click on button

first it check that this image exist or not. for eg(path is "C:/abc/sun.jpeg") it works when we debugg from application. but when we deploy this application then it doest find that path .i.e C:/abc/sun.jpeg" it exit from their and go to else.

if (File.Exists(fullpath))
{
      Page.ClientScript.RegisterStartupScript(GetType(), "Message.....", "alert('Image  found');", true);
}
else
{
    Page.ClientScript.RegisterStartupScript(GetType(), "Message.....", "alert('Image not found');", true);
    return;
}

推荐答案

首先,你必须包括所有的ima ges进入你的项目,prefrebly进入图像文件夹。然后使用Server.MapPath查找我们的图像存在。 Server.MapPath返回图像文件的物理路径,以便您可以使用它来验证存在。

First of all, you must include all the images into your project, prefreably into images folder. Then use Server.MapPath to find our the image existance. Server.MapPath return the physical path of the image file so that you can use it to verify the existance.
string imgpath = Server.MapPath("~\images\yourimg.jpg");

if(System.IO.File.Exists(imgpath))
...
else
...


这篇关于检查文件是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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