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?
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?
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.
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.
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");