Comparing two values - vs2005

  • Thread starter Thread starter Rick
  • Start date Start date
R

Rick

I have a string value 5.2.1 and I need to know how to compare if this value
is greater than or equlas to 5.1.4
 
Rick pretended :
I have a string value 5.2.1 and I need to know how to compare if this value
is greater than or equlas to 5.1.4

Load them in a Version class, then you can compare them.

This works only if
a) the parts are separated by a '.'
b) the parts are integers (no "1.2a")
c) there are not more than 4 parts (less is fine)

Hans Kesting
 
Peter said:
If you could explain what about the String.Compare() method doesn't
accomplish your goal, that would help us provide a better answer.

If standard compare does not work, then it is probably because
the parts are integer meaning that 1.1.10 comes after 1.1.9.

Arne
 
I will try to make it more clear of what I am trying to accomplish:
I may have a product version e.g. 1.2.3.4 this could be any number
I need to to know if this numer is greater or equal to a constant version
number 1.2.1.1
 
I have a string value 5.2.1 and I need to know how to compare if this
value is greater than or equlas to 5.1.4

Why not just split the string by the periods, and compare each number
individually? If your numbers are always x.y.z format, and assuming
that the versioning scheme is standard linear, the following function
should work:

/*
* Take two version numbers, in x.y.z format, and compare them.
* Returns 0 if equal, 1 if v1 > v2, 255 if v1 < v2. Must be
* made resilient to invalid input.
*/
public static byte VersionCompare(string v1, string v2) {
int[] v1Numbers = new int[3];
int[] v2Numbers = new int[3];
int counter = 0;

foreach(string aNumber in v1.Split(new char[] { '.' })) {
v1Numbers[counter++] = int.Parse(aNumber);
}

counter = 0;
foreach(string aNumber in v2.Split(new char[] { '.' })) {
v2Numbers[counter++] = int.Parse(aNumber);
}

if(v1Numbers[0] > v2Numbers[0])
return(1);

if(v1Numbers[0] == v2Numbers[0] &&
v1Numbers[1] > v2Numbers[1])
return(1);

if(v1Numbers[0] == v2Numbers[0] &&
v1Numbers[1] == v2Numbers[1] &&
v1Numbers[2] > v2Numbers[2])
return(1);

if(v1Numbers[0] == v2Numbers[0] &&
v1Numbers[1] == v2Numbers[1] &&
v1Numbers[2] == v2Numbers[2])
return(0);

return(255);
}

--- Mike
 
As Hans suggested; I've already tried the following code but it won't work.
It compiles fine but generate an error "An exception has been thrown by the
target of an invocation" when application is launched.
Assembly myAsm = Assembly.Load("MyApplicationNameWithoutDotEXE");
AssemblyName aName = myAsm.GetName();
Version ver = aName.Version;

I am trying to compare if my current aplication version is older, equal or
greater than a specific version number 5.2.6
 
Pete,
The sample code you provided gives the assembly info for the executing
TestAssemblyVersion application only; I couldn't use the same application to
give me the assembly info for other applications. e.g.:

using System;
using System.Reflection;

namespace TestAssemblyVersion
{
class Program
{
static void Main(string[] args)
{
// Assembly assembly = Assembly.GetExecutingAssembly();
Assembly assembly = Assembly.Load("WinWord");
Console.WriteLine("\"{0}\"",
assembly.GetName().Version.ToString());
Console.ReadLine();
}
}
}



Peter Duniho said:
As Hans suggested; I've already tried the following code but it won't
work.
It compiles fine but generate an error "An exception has been thrown by
the
target of an invocation" when application is launched.
Assembly myAsm = Assembly.Load("MyApplicationNameWithoutDotEXE");
AssemblyName aName = myAsm.GetName();
Version ver = aName.Version;

The following code works fine:

using System;
using System.Reflection;

namespace TestAssemblyVersion
{
class Program
{
static void Main(string[] args)
{
// Assembly assembly = Assembly.GetExecutingAssembly();
Assembly assembly = Assembly.Load("TestAssemblyVersion");

Console.WriteLine("\"{0}\"",
assembly.GetName().Version.ToString());

Console.ReadLine();
}
}
}

It seems to me that the commented line is preferable -- your assembly
should already be loaded, so why load it again? -- but either produces the
expected output.

So, you've done something wrong. You need to post a concise-but-complete
code sample showing _exactly_ what you _are_ doing, otherwise it's not
possible to understand what the mistake is.

Pete
 
Pete,
Please find below the error message that I am getting. I just need a small
utility that will check the version number of any application (executable) by
passing its name on a command line.
I really appreciate the way you have been providing me the support; thank
you so much.

ERROR:
An unhandled exception of type 'System.IO.FileNotFoundException' occurred in
mscorlib.dll

Additional information: File or assembly name WinWord.exe, or one of its
dependencies, was not found.
 
Is it the same for a dll residing in a local machine?
I have seen applications complaining about existing dlls being old; how do
they check version number on an existing dll?
 
Back
Top