< Day Day Up > |
TechniqueThe 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 . CommentsOne 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 > |