<p>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.</p>

<pre><code>if (System.IO.Directory.Exists(photosLocation))
{
    string[] files = System.IO.Directory.GetFiles(photosLocation, "*.jpg");
    if (files.Length &gt; 0)
    {
        // TODO: return the url of the first file found;
    }
}
</code></pre>

[{'answer_id': 16031, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>So far as I know there\'s no single function which does this (maybe you were looking for the inverse of <a href="http://msdn.microsoft.com/en-us/library/ms524632.aspx" rel="nofollow noreferrer">MapPath</a>?). 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.</p>\n'}, {'answer_id': 16032, 'author': 'Robin Robinson', 'author_id': 1629, 'author_profile': 'https://Stackoverflow.com/users/1629', 'pm_score': -1, 'selected': False, 'text': '<p>I think this should work. It might be off on the slashes.  Not sure if they are needed or not.</p>\n\n<pre><code>string url = Request.ApplicationPath + "/" + photosLocation + "/" + files[0];\n</code></pre>\n'}, {'answer_id': 16039, 'author': 'Fredrik Kalseth', 'author_id': 1710, 'author_profile': 'https://Stackoverflow.com/users/1710', 'pm_score': 5, 'selected': True, 'text': '<p>As far as I know, there\'s no method to do what you want; at least not directly. I\'d store the <code>photosLocation</code> as a path relative to the application; for example: <code>"~/Images/"</code>. This way, you could use MapPath to get the physical location, and <code>ResolveUrl</code> to get the URL (with a bit of help from <code>System.IO.Path</code>):</p>\n\n<pre><code>string photosLocationPath = HttpContext.Current.Server.MapPath(photosLocation);\nif (Directory.Exists(photosLocationPath))\n{\n    string[] files = Directory.GetFiles(photosLocationPath, "*.jpg");\n    if (files.Length &gt; 0)\n    {\n        string filenameRelative = photosLocation +  Path.GetFilename(files[0])   \n        return Page.ResolveUrl(filenameRelative);\n    }\n}\n</code></pre>\n'}, {'answer_id': 16040, 'author': 'Biri', 'author_id': 968, 'author_profile': 'https://Stackoverflow.com/users/968', 'pm_score': 2, 'selected': False, 'text': '<p>Maybe this is not the best way, but it works.</p>\n\n<pre><code>// Here is your path\nString p = photosLocation + "whatever.jpg";\n\n// Here is the page address\nString pa = Page.Request.Url.AbsoluteUri;\n\n// Take the page name    \nString pn = Page.Request.Url.LocalPath;\n\n// Here is the server address    \nString sa = pa.Replace(pn, "");\n\n// Take the physical location of the page    \nString pl = Page.Request.PhysicalPath;\n\n// Replace the backslash with slash in your path    \npl = pl.Replace("\\\\", "/");    \np = p.Replace("\\\\", "/");\n\n// Root path     \nString rp = pl.Replace(pn, "");\n\n// Take out same path    \nString final = p.Replace(rp, "");\n\n// So your picture\'s address is    \nString path = sa + final;\n</code></pre>\n\n<p>Edit: Ok, somebody marked as not helpful. Some explanation: take the physical path of the current page, split it into two parts: server and directory (like c:\\inetpub\\whatever.com\\whatever) and page name (like /Whatever.aspx). The image\'s physical path should contain the server\'s path, so "substract" them, leaving only the image\'s path relative to the server\'s (like: \\design\\picture.jpg). Replace the backslashes with slashes and append it to the server\'s url.</p>\n'}, {'answer_id': 16188, 'author': 'Andy Rose', 'author_id': 1762, 'author_profile': 'https://Stackoverflow.com/users/1762', 'pm_score': 3, 'selected': False, 'text': "<p>I've accepted Fredriks answer as it appears to solve the problem with the least amount of effort however the Request object doesn't appear to conatin the ResolveUrl method.\nThis can be accessed through the Page object or an Image control object:</p>\n\n<pre><code>myImage.ImageUrl = Page.ResolveUrl(photoURL);\nmyImage.ImageUrl = myImage.ResolveUrl(photoURL);\n</code></pre>\n\n<p>An alternative, if you are using a static class as I am, is to use the VirtualPathUtility:</p>\n\n<pre><code>myImage.ImageUrl = VirtualPathUtility.ToAbsolute(photoURL);\n</code></pre>\n"}, {'answer_id': 6872552, 'author': 'fireydude', 'author_id': 869290, 'author_profile': 'https://Stackoverflow.com/users/869290', 'pm_score': 0, 'selected': False, 'text': '<p>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.</p>\n'}, {'answer_id': 8985747, 'author': 'NxtWhat', 'author_id': 1166821, 'author_profile': 'https://Stackoverflow.com/users/1166821', 'pm_score': 2, 'selected': False, 'text': '<p>This worked for me:</p>\n\n<pre><code>HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpRuntime.AppDomainAppVirtualPath + "ImageName";\n</code></pre>\n'}, {'answer_id': 12897607, 'author': 'Rafael Herscovici', 'author_id': 572771, 'author_profile': 'https://Stackoverflow.com/users/572771', 'pm_score': 4, 'selected': False, 'text': '<p>this is what i use:</p>\n\n<pre><code>private string MapURL(string path)\n{\n    string appPath = Server.MapPath("/").ToLower();\n    return string.Format("/{0}", path.ToLower().Replace(appPath, "").Replace(@"\\", "/"));\n }\n</code></pre>\n'}, {'answer_id': 13101286, 'author': 'Ross Presser', 'author_id': 864696, 'author_profile': 'https://Stackoverflow.com/users/864696', 'pm_score': 4, 'selected': False, 'text': '<p>The problem with all these answers is that they do not take virtual directories into account.</p>\n\n<p>Consider:</p>\n\n<pre><code>Site named "tempuri.com/" rooted at c:\\domains\\site\nvirtual directory "~/files" at c:\\data\\files\nvirtual directory "~/files/vip" at c:\\data\\VIPcust\\files\n</code></pre>\n\n<p>So:</p>\n\n<pre><code>Server.MapPath("~/files/vip/readme.txt") \n  = "c:\\data\\VIPcust\\files\\readme.txt"\n</code></pre>\n\n<p>But there is <em>no</em> way to do this:</p>\n\n<pre><code>MagicResolve("c:\\data\\VIPcust\\files\\readme.txt") \n   = "http://tempuri.com/files/vip/readme.txt"\n</code></pre>\n\n<p>because there is no way to get a complete list of virtual directories.</p>\n'}, {'answer_id': 13452579, 'author': 'adripanico', 'author_id': 1552386, 'author_profile': 'https://Stackoverflow.com/users/1552386', 'pm_score': 0, 'selected': False, 'text': '<p>For get the left part of the URL:</p>\n\n<pre><code>?HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority)\n"http://localhost:1714"\n</code></pre>\n\n<p>For get the application (web) name:</p>\n\n<pre><code>?HttpRuntime.AppDomainAppVirtualPath\n"/"\n</code></pre>\n\n<p>With this, you are available to add your relative path after that obtaining the complete URL.</p>\n'}]
['https://Stackoverflow.com/questions/16007', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1762/']