views:

217

answers:

2

Example:

MyProgram.exe is executed. It calls MyClassLibrary1.dll which calls MyClassLibrary2.dll. How can I determine from within MyClassLibrary2.dll what the assembly version of MyProgram.exe is?

Is this possible?

Thanks.

+5  A: 
System.Reflection.Assembly.GetEntryAssembly().GetName().Version
Mehrdad Afshari
Thanks! As an aside - do you have any tips on getting familiar with the framework so this type of stuff is just second nature?
vg1890
@vg1890: MSDN was the best resource I could learn from. It takes time and experience with the framework. There is a "CLR via C#" book that teaches about the CLR internals. I haven't read it but I have heard many good reviews about it.
Mehrdad Afshari
A: 

EDIT My answer will only work if the DLL has a reference to the actually EXE which would be rather odd to do.

Try the following

typeof(SomeTypeInMyProgram).Assembly.GetName().Version

This will return a Version structure that you can use to analyze the version of MyProgram.exe.

JaredPar
How can you do that? It'll be a circular reference, I think.
Mehrdad Afshari
It works just fine - any type object will have a reference to its own assembly, from there you can determine anything about that assembly including its version.
Andrew Hare
The problem is it's not its own assembly. MyProgram depends on MyClassLibrary1 which in turn depends on MyClassLibrary2. To detect the version of MyProgram from MyClassLibrary2 in this way, you should have it available at compile time.
Mehrdad Afshari
But MyClassLibrary2 needs a reference to MyProgram.exe for this to work.
Dave Van den Eynde
@Mehrdad: indeed.
Dave Van den Eynde
@Mehrdad, doh. Forgot about that.
JaredPar
@JaredPar: Is it really possible? Since you don't have the EXE available at compile time (it should be built *after* the DLL) and the DLL requires the EXE to build. I'm curious if it's possible can make it work with a dummy EXE or something :-?
Mehrdad Afshari
@Mehrdad, it's certainly possible to reference an EXE. You can use asmmeta files to get around the circular dependencies.
JaredPar
@JaredPar: Thanks for the point. Yeah. I have done it with Reflector.exe before. I haven't had worked with `asmmeta` files before, but it seems the basic strategy is to have some dummy assemblies to resolve references at compile time.
Mehrdad Afshari