简体   繁体   English

Asp.net c# - 检查文件是否存在失败

[英]Asp.net c# - Checking if file exist failed

I have a project where I want to check if a file exist in a directory. 我有一个项目,我想检查目录中是否存在文件。

if (File.Exists("\\Galerija\\" + Session["up_ime"] + "\\" + Session["slika_profila"]))
{
  Label1.Text = "exist";
}
else {
  Label1.Text = "does not exist";
}

The directory of the file is \\Galerija\\admin\\slika.jpg . 该文件的目录是\\Galerija\\admin\\slika.jpg The file is in the directory, but I get returned that it doesn't. 该文件位于目录中,但我返回它没有。 I also printed the path I give to File.Exists() to the label and it should be correct. 我还将我给File.Exists()的路径打印到标签上,它应该是正确的。 I don't see what is the problem. 我不明白这是什么问题。 -The file should be found. - 应该找到该文件。

In your comments you mentioned this is web site. 在您的评论中,您提到这是网站。 So try with Server.MapPath 因此,尝试使用Server.MapPath

if (File.Exists(Server.MapPath(string.Format("Galerija/{0}/{1}" ,Session["up_ime"] , Session["slika_profila"]))))
{
     Label1.Text = "exist";

}

Your relative path should be incorrect, Use Server.MapPath("your relative path to root folder") instead of direct relative path, 您的相对路径应该不正确,使用Server.MapPath(“您的根文件夹的相对路径”)而不是直接相对路径,

In your case, use 根据您的情况使用

if (File.Exists(Server.MapPath("\\Galerija\\" + Session["up_ime"] + "\\" + Session["slika_profila"])))
        {
            Label1.Text = "exist";

        }
        else
        {
            Label1.Text = "does not exist";
        }

You can find more info here, 您可以在此处找到更多信息,

http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath.aspx http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath.aspx

If you're still not able to figure out what is wrong, you should use procmon.exe to find out the path that the file is being looked up at. 如果仍然无法找出问题所在,则应使用procmon.exe找出正在查找文件的路径。 It will also show you the errors if there are any. 如果有任何错误,它也会向您显示错误。 procmon outputs a lot of information, but the filter functionality can help you here. procmon输出了很多信息,但过滤器功能可以帮助你。

Download it here - http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx 在这里下载 - http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM