File.Exists(file) is false but file exist

asp.net-mvc-4, c#, file

Solution

You need to do the `Server.MapPath` again when checking the file and do the forward slash.

string file = Server.MapPath("~") + @"\App_Data\Plakaty\" 
    + wyd.EventId.ToString() + ".jpg";

ViewBag.file_exist = System.IO.File.Exists(file ); //always is false

Problem

I'm add a file in one controller and in another controller I want check if the file is exist. I' using `File.Exist(file)`, but it's always false, even if the file exist... I adding file, and image is added successful. ``` if ((image!= null & image.ContentLength > 0)) { string name = event.EventId.ToString() + ".jpg"; var fileName = name; var path = Path.Combine(Server.MapPath("~/App_Data/Plakaty"), fileName); plakat.SaveAs(path); } ``` I'm checking in another controller if this file exist: ``` string file = "~/App_Data/Plakaty/" + wyd.EventId.ToString() + ".jpg"; ViewBag.file_exist = System.IO.File.Exists(file); //always is false ``` And my View: (It's returning only "No file") ``` @if (ViewBag.file_exist == true) { <p>File exist</p> } else { <p>No file</p> } ```

Original source