Wednesday, December 30, 2009

Display VS Tools Assembly Version

(Update: Reader Stephan offers a much more refined solution in the comments section of this post)

Recently I've been buried writing a VS Tools AddIn that has been a bit more complex than any previous AddIn that I've had to develop. Not terribly complex, just more involved than the typical AddIn widget that I write frequently. It's basically like a mini-module: A custom window, with a custom GP menu item, its own custom table, and lookups that query from GP tables.

And since the client uses C# internally, I've been transitioning to C# as well, so it's only the second project that I've done in C#.

Anyway, one of the things that I like to have on any user interface application that I develop is a visible build number. I have never used Help -> About dialogs (although maybe I should), so I just prefer to use a label or status strip to discretely show the app version.

Well, with VS Tools AddIns, this is a bit tricky.

There are at least 3 different ways that I've found to try and get the version number of your .NET application. Which approach you use depends on the type of app you've written (WinForm vs. non-WinForm, and other subtleties that I don't fully understand). With a VSTools AddIn, the 3 approaches produce different results, only one of which is truly the Assembly Version of my DLL.

One approach (Application.ProductVersion) displayed 10.0.0357 (or something like that), as it was apparently referencing some GP component that was invoking my DLL.

Another (System.Reflection.Assembly.GetCallingAssembly) simply displayed 2.0.0.0. I'm not entirely sure what that was referring to, but from what I could tell, it might have been a base .NET assembly version, like Windows.Forms.

And finally, #3, the winner, produced the version info I was looking for. After googling through several results, I found it on this web page in VB syntax.

Having taken the C# plunge, I ended up writing it this way:

StringBuilder buildNumber = new StringBuilder("Build: "); buildNumber.Append(System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).FileMajorPart);
buildNumber.Append(".");
buildNumber.Append(System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).FileMinorPart);
buildNumber.Append(".");
buildNumber.Append(System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).FileBuildPart);
buildNumber.Append(".");
buildNumber.Append(System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).FilePrivatePart);

lblBuild.Text = buildNumber.ToString();


There are probably a few other ways to do it, and probably more elegant ways of assembling the string, but I had a deadline, so this was good enough for now.

Over the next week I will try and share some tips and tricks that I used to get around some VS Tools limitations and a VS Tools bug.

3 comments:

Stephan said...

I also ran into this issue with my .NET VS Tools add-ins, but I came across a similar method. I'll share it in case it's of interest to anyone.

VB
Dim currentAssembly = System.Reflection.Assembly.GetExecutingAssembly()
Dim _currentVersion = currentAssembly.GetName().Version

C#
System.Reflection.Assembly currentAssembly = System.Reflection.Assembly.GetExecutingAssembly();
System.Version _currentVersion = currentAssembly.GetName().Version;

From there, the _currentVersion variable can be used in whatever manner necessary.

Thanks!
Stephan Desmoulin

Steve Endow said...

Thank you Stephan, so much cleaner!

I think I've had to look this up several times, and each time I stumble across a different blog post or web page that has a different technique.

Yours 2-line approach nice and simple.

Thanks!

Steve

Stephan said...

My pleasure, Steve. I always enjoy helping spread the knowledge in the programming world. But I must thank you for the post, because a post about this particular subject is very rare. Thanks!