Convert a Windows path to a URL

public string ConvertWindowsPathToUrl(string WindowsPathAndFilename)
{
 string ReturnValue = WindowsPathAndFilename;
 //Make sure the file really exists.
 if (My.Computer.FileSystem.FileExists(WindowsPathAndFilename) == true) {
  IO.FileInfo WindowsFileInfo = My.Computer.FileSystem.GetFileInfo(WindowsPathAndFilename);
  dynamic WindowsFullFilename = WindowsFileInfo.FullName;
  string ThisPageWindowsPath = Request.PhysicalPath;
  IO.FileInfo ThisPageFileInfo = My.Computer.FileSystem.GetFileInfo(ThisPageWindowsPath);
  string ThisPageFolder = ThisPageFileInfo.Directory.FullName;
  string ThisPageUrl = Page.ResolveClientUrl(Request.CurrentExecutionFilePath);
  string ThisPageUrlFolder = System.Web.VirtualPathUtility.GetDirectory(ThisPageUrl);
  if (WindowsFullFilename.Contains(ThisPageFolder) == true) {
   //Only return a URL if the path to the file is the same as or under the path to this page.
   string UrlText = WindowsFullFilename;
   UrlText = UrlText.Replace(ThisPageFolder + "\\", ThisPageUrlFolder);
   UrlText = UrlText.Replace("\\", "/");
   UrlText = System.Uri.EscapeUriString(UrlText);
   ReturnValue = UrlText;
  }
 }
 return ReturnValue;
}

No comments:

Post a Comment