19.11 Identifying the File or URL from Which an Assembly Was Loaded

 <  Day Day Up  >  

You want to find out programmatically where an assembly was loaded from.


Technique

The technique depends on whether you explicitly want the file path on the local computer or you need to know the original source of the assembly.

If you need the exact file path to the file where the assembly was directly loaded, you should use the Assembly.Location property:

 
 Assembly execAssembly = Assembly.GetExecutingAssembly(); Console.WriteLine(execAssembly.Location); 

The Location property returns a string giving the exact file path in a format such as C:\CookbookSamples\MyLibrary.dll . In some cases, this format is what you want, but in other cases, it might be misleading. For example, if an assembly was downloaded from the Internet, it is first copied to the local machine and stored in a special download section of the assembly cache before being executed. The Assembly.Location property in this case returns the exact path of the local copy inside the assembly cache. If what you really need to know is the URL where the assembly was originally downloaded, you should use the Assembly.CodeBase property. For example, to display the original location of the currently executing assembly, you use the following:

 
 Assembly execAssembly = Assembly.GetExecutingAssembly(); Console.WriteLine(execAssembly.CodeBase); 

For assemblies whose origin is the local machine, CodeBase returns a URL of the form file:///C:/CookbookSamples/MyLibrary.dll , but for assemblies downloaded over the Internet, CodeBase returns a URL of the form http://www.SomeDomain.com/FileWasHere/MyLibrary.dll .

Comments

One useful point is that AssemblyName also exposes a property, CodeBase , which normally returns the same string as Assembly.CodeBase , but without the need to have the assembly loaded into the current application domain.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net