Solved

Getting running version info from another assembly

Posted on 2014-04-14
3
202 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
  • 2
3 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
kaufmed gave the general solution. I needed something more direct, so used included my solution.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sort multidimensional array in c# 2 42
How do I get the id from URL? 19 46
Windows Service with UDP 2 26
comparing string in C# 3 32
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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now