Solved

Getting running version info from another assembly

Posted on 2014-04-14
3
209 Views
Last Modified: 2014-04-20
I'm using this code to give me version info and build date-time (using "assembly: AssemblyVersion("1.1.*")]"). I'd like to be able to get this info with a call from a different assembly, so that it will give me the info for that assembly. How can I do that?
public static class VersionInfo {
        /// <summary>
        /// Current build Major.Minor version info
        /// </summary>
        public static string Version {
            get { return RunningVersion.Major + "." + RunningVersion.Minor; }
        }
        /// <summary>
        /// Current build DateTime
        /// </summary>
        public static DateTime BuildDateTime {
            get {
                return new DateTime(2000, 1, 1).Add(
                   new TimeSpan(TimeSpan.TicksPerDay * RunningVersion.Build + // Days since 1 Jan 2000
                       TimeSpan.TicksPerSecond * 2 * RunningVersion.Revision));    // Seconds since midnight
            }
        }
        private static readonly Version RunningVersion = Assembly.GetExecutingAssembly().GetName().Version;
    }

Open in new window

0
Comment
Question by:BlearyEye
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39999348
Use the AppDomain class:

e.g.

Version v = AppDomain.CurrentDomain.GetAssemblies()
                                   .Where(a => a.FullName.StartsWith("ClassLibrary1"))
                                   .First()
                                   .GetName()
                                   .Version;

Open in new window


Be aware that GetAssemblies returns the assemblies that have actually been loaded. You may have an assembly being used by your project that hasn't yet been loaded by the runtime at the point in which you call GetAssemblies. If there is a chance that you have this going on, then you need to either cause the assembly to be loaded--by using some of its types--or by explicitly loading the assembly into the AppDomain.
0
 
LVL 1

Assisted Solution

by:BlearyEye
BlearyEye earned 0 total points
ID: 39999418
I may have come up with a simpler approach:
    public static class VersionInfo {

        /// <summary>
        /// The version for which info is required
        /// </summary>
        private static Version _theVersion = Assembly.GetExecutingAssembly().GetName().Version;

        /// <summary>
        /// Initialize the version
        /// </summary>
        /// <remarks>Skip this if you want version info from the assembly in which this class resides</remarks>
        /// <param name="theAssembly"></param>
        public static void Init(Assembly theAssembly) {
            _theVersion = theAssembly.GetName().Version;
        }

        /// <summary>
        /// Current build Major.Minor version info
        /// </summary>
        public static string Version {
            get { return _theVersion.Major + "." + _theVersion.Minor; }
        }


        /// <summary>
        /// Current build DateTime
        /// </summary>
        public static DateTime BuildDateTime {
            get {
                return new DateTime(2000, 1, 1).Add(
                   new TimeSpan(TimeSpan.TicksPerDay * _theVersion.Build + // Days since 1 Jan 2000
                       TimeSpan.TicksPerSecond * 2 * _theVersion.Revision));    // Seconds since midnight
            }
        }

    }

Open in new window

I can call Init first:
VersionInfo.Init(Assembly.GetExecutingAssembly());

Open in new window

After than I can get version info.

Your solution is more general and would work for any assembly in the solution.
0
 
LVL 1

Author Closing Comment

by:BlearyEye
ID: 40011188
kaufmed gave the general solution. I needed something more direct, so used included my solution.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question