Click here to Skip to main content
15,884,298 members
Articles / Desktop Programming / WPF
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
1 Mar 2010CPOL 13.1K  
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Paris Photo Tools
United States United States
I was faithfully married to MFC from 1994 to 2008, developing over a million lines of code for desktop applications during that time. Pockets of my employer at that time started flirting with WPF and I myself abandoned my longtime MFC wife and took up an affair with WPF, with which I've been infatuated ever since. Not that I wouldn't be willing to move back in with my wife for the right offer. But I still hold out the hope that WPF will be able to support me and all its other lovers over the long term.

Comments and Discussions

 
-- There are no messages in this forum --