2

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. 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. I don't see what is the problem. -The file should be found.

6
  • I tried also with the "FileInfo fi = new FileInfo("\\Galerija" + Session["up_ime"] + "\\" + Session["slika_profila"]);" and "fi.exists" but the same problem Commented May 30, 2013 at 18:42
  • 1
    You're probably getting the relative path wrong. Try an absolute path. Commented May 30, 2013 at 18:44
  • 1
    Is Galerija in the application's directory? Commented May 30, 2013 at 18:44
  • Absolute path works, but i would need a relative path... Galerija is in the applications directory, yes. Commented May 30, 2013 at 18:46
  • Then you're definitely getting the relative path wrong. Fix that and you're good to go. What's the issue with this solution? We don't know your folder structure so this is pretty difficult to answer. Commented May 30, 2013 at 18:48

3 Answers 3

6

In your comments you mentioned this is web site. So try with Server.MapPath

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

}
Sign up to request clarification or add additional context in comments.

Comments

1

Your relative path should be incorrect, Use Server.MapPath("your relative path to root folder") instead of direct relative path,

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

Comments

0

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. 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.

Download it here - http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.