Basically I have some code to check a specific directory to see if an image is there and if so I want to assign a URL to the image to an ImageControl.
if (System.IO.Directory.Exists(photosLocation))
{
string[] files = System.IO.Directory.GetFiles(photosLocation, "*.jpg");
if (files.Length > 0)
{
// TODO: return the url of the first file found;
}
}
So far as I know there's no single function which does this (maybe you were looking for the inverse of MapPath?). I'd love to know if such a function exists. Until then, I would just take the filename(s) returned by GetFiles, remove the path, and prepend the URL root. This can be done generically.
As far as I know, there's no method to do what you want; at least not directly. I'd store the
photosLocation
as a path relative to the application; for example:"~/Images/"
. This way, you could use MapPath to get the physical location, andResolveUrl
to get the URL (with a bit of help fromSystem.IO.Path
):The simple solution seems to be to have a temporary location within the website that you can access easily with URL and then you can move files to the physical location when you need to save them.
For get the left part of the URL:
For get the application (web) name:
With this, you are available to add your relative path after that obtaining the complete URL.