System.IO.File will work if you specify an absolute or relative path. The relative path will be relative not to the HTML root folder, but to the current working directory. The current working directory will be of type C:\Program Files (x86)\IIS Express .
The ~ character at the beginning of the file path is interpreted only as part of the current ASP.NET context, of which the File methods do not know anything.
If you are in the controller method, you can use the HttpContext.Server object, otherwise (for example, in the view) you can use HttpContext.Current.Server .
var relativePath = "~/files/downloads/" + fileCode + ".pdf"; var absolutePath = HttpContext.Server.MapPath(relativePath); if(System.IO.File.Exists(absolutePath)) ....
source share