5

How can I do it? I followed this tutorial, so I have this method:

if (getOSInfo() >= "7")
{
    MessageBox.Show("Your Microsoft Windows version isn't supported.\n\nPlease use Windows 7 or above to be able to use this program.");
    Application.Current.Shutdown();
}

It gives an error:

Cannot apply operator >= to operands of type string and string

Siavash
  • 2,813
  • 4
  • 29
  • 42
  • method `getOSInfo()` I have posted already (as a link to the source) https://andrewensley.com/2009/06/c-detect-windows-os-part-1/ and the error is `Cannot apply operator >= to operands of type string and string` –  Oct 27 '18 at 20:43
  • What is so difficult to understand with the rather clear error message "_Cannot apply operator >= to operands of type string and string_"? You cannot use relational operators such as <, >, <= and >= with strings. Use string.Compare(...). –  Oct 27 '18 at 20:48
  • I can't post all code into the question, because my question content becomes mostly code and I can't save it. –  Oct 27 '18 at 20:48
  • The method you link to returns names like "Vista" , "7" and "2000". That is not compatible with `>=` or `String.Compare()`. In .Net, use Environment.OsVersion and find your cutoff point. And if it's not NT, is't before "7". – H H Oct 27 '18 at 21:32

1 Answers1

3

Because the method getOSInfo return string data-type and it included "98" , "ME" , "XP" and you can't compare it with >= operator, you can change it to below code:

if(Environment.OSVersion.Version.Major >= 6 && Environment.OSVersion.Version.Minor >= 1)
{
    // whatever you want to do...
}

And as @nelek mentioned, this is a comprehensive article about operation system versions:

https://learn.microsoft.com/en-us/windows/desktop/SysInfo/operating-system-version

Siavash
  • 2,813
  • 4
  • 29
  • 42
  • 1
    And application will be quit if Windows version is any version but 7... Imagine some application support Win7, but not WinXP and Win8, 10... Little strange, isn't? – nelek Oct 28 '18 at 07:21
  • @nelek you right, I corrected my answer – Siavash Oct 28 '18 at 07:27
  • 1
    Again me ;) ... You have to change value for `Version.Minor > 1` to `>0`, because `Minor=1` is Win7... User application will support Win7 also.. You can see here list of Win versions : https://learn.microsoft.com/en-us/windows/desktop/SysInfo/operating-system-version – nelek Oct 28 '18 at 07:37
  • @ I was too distracted these days :( – Siavash Oct 28 '18 at 07:42
  • @nelek I read that page, and that was so helpful, thank you. – Siavash Oct 28 '18 at 07:48
  • 1
    Note how your snippet misbehaves when they release version 7.0 Otherwise a very good demonstration why Microsoft has to lie about version numbers :) – Hans Passant Oct 28 '18 at 12:10