views:

286

answers:

2

Assembly.Location gives a plain path to the assembly. Unfortunately this is empty when running in a shadowed environment, such as unit test or ASP.NET. Hovever, the Codebase property is available and provides a URI that can be used instead. In which cases it returns no URI starting with file:///? Or in other words: what are the cases in which this won't work or will return unusable results?

Assembly assembly = GetType().Assembly;    
Uri codeBaseUri = new Uri(assembly.CodeBase);
string path = codeBaseUri.LocalPath;
+2  A: 

It's possible to load assemblies directly over HTTP, such as in a ClickOnce deployment:

Assembly assembly = Assembly.LoadFrom("http://server/app.dll");
Uri codeBaseUri = new Uri(assembly.CodeBase);
Debug.Assert(codeBaseUri.Scheme == "http"); 
Debug.Assert(codeBaseUri.LocalPath == "");
Debug.Assert(assembly.Location == "");
Tim Robinson
Never used this directly, although I used ClickOnce before. You're probably right, but what if my code would be inside your app.dll and the app.dll was downloaded the way you described. What am I getting in path? %LOCALAPPDATA%\Apps\2.0\something? Or would it fail completely?
Marc Wittke
path (i.e. Uri.LocalPath) is empty unless the URI contains a file:// link. Likewise, assembly.Location is also blank. I thought I had another property for finding the shadow-copied location (which is guaranteed to be a real file), but I can't see it just now.
Tim Robinson
A: 

Also, # char is threated as a fragment separator. See http://blogs.msdn.com/ncl/archive/2010/02/23/system-uri-f-a-q.aspx (section 9) for short explanation.

Use Assembly.EscapedCodeBase instead of Assembly.CodeBase to fight it.

Sinix