Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Getting the Version of an Assembly embedded as a WPF resource.

0.00/5 (No votes)
1 Mar 2010 1  
I have an assembly embedded as a resource in my application. I had the need to get the System.Version of that assembly. I figured that should be easy but off the top of my head I didn't know the series of calls needed to accomplish this. So I decided to sit down and walk through the problem....
I have an assembly embedded as a resource in my application. I had the need to get the System.Version of that assembly. I figured that should be easy but off the top of my head I didn't know the series of calls needed to accomplish this. So I decided to sit down and walk through the problem. This is the thought process I went through in coming up with the solution.
I knew how to get the version of the executing assembly:
Assembly assembly = Assembly.GetAssembly(GetType());
AssemblyName assemblyName = assembly.GetName();
Version version = assemblyName.Version;

So if I could figure out how to get the Assembly object of the assembly embedded in my resources, it would be a trivial exercise. Well, I also knew how to get a Stream for the embedded resource:
Uri uri = new Uri("Resources/" + resourceFileName,UriKind.Relative);
StreamResourceInfo sri = Application.GetResourceStream(uri);
Stream componentStream = sri.Stream;

So next I had to look for a way of getting an Assembly from a Stream. Looking through the methods in the Assembly I found the promising-sounding Assembly.ReflectionOnlyLoad(Byte[]). I figured that would be useful because I knew how to get a Byte[] from a Stream:
Byte[] bytes = new Byte[componentStream.Length];
int size = componentStream.Read(bytes, 0, (int omponentStream.Length);

Then I figured I could get the assembly like this:
Assembly assembly = Assembly.ReflectionOnlyLoad(bytes);

Put it all together and you come up with this, which works!
Uri uri = new Uri("Resources/PhotoHelpDesk.exe", UriKind.Relative);
StreamResourceInfo sri = Application.GetResourceStream(uri);
Stream componentStream = sri.Stream;
Byte[] bytes = new Byte[componentStream.Length];
int size = componentStream.Read(bytes, 0, (int)componentStream.Length);
Assembly assembly = Assembly.ReflectionOnlyLoad(bytes);
AssemblyName assemblyName = assembly.GetName();
Version version = assemblyName.Version;

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here