I need to access the assembly of my project in C# .NET2.0.
I can see the GUID in the 'Assembly Information' dialog in under project properties, and at the moment I have just copied it to a const in the code. The GUID will never change, so this is not that bad of a solution, but it would be nice to access it directly. Is there a way to do this?
Edit: To those who insist on downvoting... Unable to delete this answer because it is the accepted version. Therefore, am editing to include the correct answer (JaredPar's code below)
Simple enough if you only want to get the Executing assembly:
You should be able to read the Guid attribute of the assembly via reflection. This will get the GUID for the current assembly
You can replace the GuidAttribute with other attributes as well, if you want to read things like AssemblyTitle, AssemblyVersion etc
You can also load another assembly (Assembly.LoadFrom and all) instead of getting the current assembly - if you need to read these attributes of external assemblies (eg - when loading a plugin)
Try the following code. The value you are looking for is stored on a GuidAttribute instance attached to the Assembly
In case anyone else is looking for an out of the box working example, this is what I ended up using based on the previous answers.
Update:
Since this has gotten a little bit of attention I decided to include another way of doing it I've been using. This way allows you to use it from a static class:
Or, just as easy:
Works for me...
To get the appID you could use the following line of code:
For this you need to include the
System.Runtime.InteropServices;