I need to retrieve the major and minor version of the application, and instead of instantiating a new Version class with new Version(Application.ProductVersion); is there a static copy of the Version class that already exists? If so, I can't find it, can someone point me in that direction, or is it only possible for me to manipulate the Application.ProductVersion string like above?

Recommended Answers

All 2 Replies

using System;
using System.Reflection;

public class Example {
   public static void Main() {
      // Get the version of the current assembly.
      Assembly assem = Assembly.GetExecutingAssembly();
      AssemblyName assemName = assem.GetName();
      Version ver = assemName.Version;
      Console.WriteLine("{0}, Version {1}", assemName.Name, ver.ToString());
   }
}

There is no static version, as any executing assembly has many versions (which version of the .NET library are you using, which version of a DLL, which version of the starting assembly, which version of the running assembly, even which version of the OS are you using). The code above gets the running assembly version.

Hmm. I suppose that's true, but writing Application.ProductVersion will return a string that shows the major, minor, revision and build number in the x.x.x.x format, I just want the first two sets for the executable.

I just assumed since I can write that and get more information than I need, I could just retrieve a small part of it easier.

I understand that there are many versions running at once, but I am just talking about the main executable, nothing else. It's not for anything complex, just for an about box and the title bar of the application.

Thanks for the help and information as always!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.