I ran into an instance of using Server.MapPath
to check for the existence of a file and since that file didn’t exist, Server.MapPath
threw an exception itself (I needed to know the path where it would be). My code that tossed the exception looked like this:
If System.IO.File.Exists(HttpContext.Current.Server.MapPath("/TestFile.txt")) = False Then
Return returnList
End If
I didn’t want to just trap the exception since that throws needless overhead. Here is a slight change that will allow the code to correctly get the Server.MapPath and not throw an exception when checking to see if the file exists:
If System.IO.File.Exists(HttpContext.Current.Server.MapPath("") & "TestFile.txt") = False Then
Return returnList
End If
In case, passing nothing into the Server.MapPath will return the root of your web-site as a local path, then you can just append the filename you want to check for that is being passed to File.Exists.