Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent System.Version from removing leading zeroes?

Tags:

c#

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?

like image 468
Marcus25 Avatar asked Mar 29 '13 16:03

Marcus25


2 Answers

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.

like image 165
D Stanley Avatar answered Nov 15 '22 15:11

D Stanley


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.

like image 30
xdumaine Avatar answered Nov 15 '22 16:11

xdumaine