Skip to content

Instantly share code, notes, and snippets.

@JayDouglass
Created July 11, 2012 15:19
Show Gist options
  • Save JayDouglass/3091084 to your computer and use it in GitHub Desktop.
Save JayDouglass/3091084 to your computer and use it in GitHub Desktop.
Physical path to website relative path ASP.NET
public static partial class FileInfoExtensions
{
public static string ToWebSiteRelativePath(this FileInfo fileInfo)
{
var applicationPhysicalPath = HostingEnvironment.ApplicationPhysicalPath;
var filePhysicalPath = fileInfo.FullName;
if (filePhysicalPath.StartsWith(applicationPhysicalPath) == false)
throw new ArgumentOutOfRangeException(filePhysicalPath + " is not within application physical path: " + applicationPhysicalPath);
return filePhysicalPath.Replace(applicationPhysicalPath, HostingEnvironment.ApplicationVirtualPath + "/").Replace("\\", "/");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment