C# – the best way to check if a website folder exists

asp.netcdirectory

I need to check if a folder exists within an ASP.NET website. This is to be done within the website itself and I need to check a few folders so want to make sure there is little overhead (ie no WebCLient calls etc)

My thinking is that I could do a HttpServerUtility.MapPath("~/") to get the root path and then a Directory.Exists(rootPath + webPath) to check the folders. Would this work for Server Farms assuming the folder structures are the same?

Is this the best way or is there some equavalent yo WebDirectory.Exists(~/mysite/somepath)

All comments welcome.

Best Answer

using System.IO;

if (Directory.Exists (Server.MapPath("~/Views/Common/")))
{
//Stuff
}