7
var versionNo = new System.Version("2.01");

I am getting the value versionNo = 2.1, but I want it as 2.01.

Any suggestions, please?

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
Marcus25
  • 863
  • 1
  • 15
  • 31

3 Answers3

13

That's how system.Version works - it stores the components of the version as separate integers, so there's no distinction between 2.01 and 2.1. If you need to display it that way you could format it as:

Version versionNo = new Version("2.01");
string s = string.Format("{0}.{1:00}",versionNo.Major, versionNo.Minor);

For convenience you could also create an extension method:

public static string MyFormat(this Version ver)
{
    return string.Format("{0}.{1:00}",ver.Major, ver.Minor);
}

That way you can still customize the display while retaining the comparability of the Version class.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
7

Each component of System.Version is an int, so it doesn't pad the 0. You could have a major, minor, and build:

Version versionNo = new Version("2.0.1");

which results in

Version = 2.0.1

but generally, 01 and 1 are equivalent. If you want to specifically have the any component of the Version 0 padded, I suggest you override the ToString method of version or create an extension method (as mentioned by D Stanley), or use a different type.

xdumaine
  • 10,096
  • 6
  • 62
  • 103
  • You: _I suggest you override the ToString method of version, [...]_ Note, however, that `System.Version` is a `sealed` class. So you can't technically `override` since you can't derive from the class. But you can write a class that wraps `System.Version`, of course. – Jeppe Stig Nielsen Mar 29 '13 at 16:36
1

If your version is 2.01, that means your MinorVersion is 01.

To output that, you'd have to pad the MinorVersion, by doing:

string version = version.Major.ToString() + version.Minor.ToString("D2");
Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148