I’m reading a file that may or may not be on the server like this:
string TheFile = System.IO.File.
ReadAllText(HttpContext.Current.Server.MapPath("~\\Scripts\\MyScript.js"));
If the file is not there, it throws an exception. I could control this with a try/catch but I was wondering if there’s a way to know if a file exists using a less dramatic approach.
Thanks for your suggestions.
You can do it this way by giving relative path in MapPath with forward slash. More about MapPath as under.
Server.MapPath(“~”) returns the physical path to the root of the
application
Server.MapPath(“/”) returns the physical path to the root
of the domain name