0

I'm having a problem with this:

Private Function Get_NT_Version()

    Dim NT As Decimal = CDec(System.Environment.OSVersion.Version.ToString.Substring(0, 3))

    MsgBox(NT)

    Return NT

End Function

I'm running on Win7 so I want to obtain this value as decimal or double: "6.1" but what I get is this: "61"

This is what I want to do:

If Get_NT_Version() < 6.0 Then

   msgbox("This application only works with an Aero compatible windows version")
   Application.Exit()

end if

UPDATE:

Tried this idea too but returns a "61"

Dim s As Double = String.Format("{0}.{1}", System.Environment.OSVersion.Version.ToString.Split(".")(0), System.Environment.OSVersion.Version.ToString.Split(".")(1))
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
  • can't you do the code as something like this instead `Private Function Get_NT_Version() As Object Dim NT As Decimal = Convert.ToDecimal(System.Environment.OSVersion.Version.ToString.Substring(0, 3)) Interaction.MsgBox(NT) Return NT End Function` – MethodMan Apr 29 '13 at 02:44
  • For all Windows 7 the entire string is "6.1.7601.65536", with the applied substring is "6.1", when I try to return a integer value to make a conditional I get a "61" instead "6.1". – ElektroStudios Apr 29 '13 at 02:48
  • I will post something that I think is all you need to get the version – MethodMan Apr 29 '13 at 02:49
  • Ok thankyou, I've tested the func of your comment but returns a 61, no matter if I need to use string.format or convert the string to any type but I can't do a conditional like this : "if string is lower than 6.1 then..." you know. – ElektroStudios Apr 29 '13 at 02:50
  • `Elektro Hacker` I will post a more in depth example for you to expand on as well aside from my answer. – MethodMan Apr 29 '13 at 03:11

2 Answers2

2

This will work for you I just tested it in C# as well as converted my C# solution over to VB for you.

Private Function Get_NT_Version() As String
    Dim NT As String = Environment.OSVersion.Version.ToString()
    MessageBox.Show(NT.Substring(0, 3))
    Return NT
End Function

if you want to take it even a step further you can utilize the following code as well

Private Sub Form1_Load(sender As Object, e As EventArgs)
    Dim str As String = Environment.OSVersion.Version.ToString()
    Dim OsName As String = ""
    If str.Contains("1.04") Then
        OsName = "Windows 1.0"
    ElseIf str.Contains("2.11") Then
        OsName = "Windows 2.0"
    ElseIf str.Contains("3") Then
        OsName = "Windows 3.0"
    ElseIf str.Contains("3.10.528") Then
        OsName = "Windows NT 3.1"
    ElseIf str.Contains("3.11 3.11") Then
        OsName = "Windows for Workgroups"
    ElseIf str.Contains("3.5 3.5.807") Then
        OsName = "Windows NT Workstation"
    ElseIf str.Contains("3.51 3.51.1057") Then
        OsName = "Windows NT Workstation"
    ElseIf str.Contains("4.0.950") Then
        OsName = "Windows 95"
    ElseIf str.Contains("4.0.1381") Then
        OsName = "Windows NT Workstation 4.0"
    ElseIf str.Contains("4.1.1998") Then
        OsName = "Windows 98"
    ElseIf str.Contains("4.1.2222") Then
        OsName = "Windows 98 Second Edition"
    ElseIf str.Contains("4.90.3000") Then
        OsName = "Windows Me"
    ElseIf str.Contains("5.0.2195") Then
        OsName = "Windows 2000 Professional"
    ElseIf str.Contains("5.1.2600") Then
        OsName = "Windows XP"
    ElseIf str.Contains("5.2.3790") Then
        OsName = "Windows XP Professional x64 Edition"
    ElseIf str.Contains("6.0.6000") Then
        OsName = "Windows Vista"
    ElseIf str.Contains("6.0.6002") Then
        OsName = "Windows Vista SP2"
    ElseIf str.Contains("6.1.7600") Then
        OsName = "Windows 7"
    ElseIf str.Contains("6.0.6000") Then
        OsName = "Windows Vista"
    ElseIf str.Contains("6.0.6000") Then
        OsName = "Windows Vista"
    End If
    MessageBox.Show(OsName + " " + Environment.OSVersion.ServicePack)
End Sub
MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • but the returned type is a string :-/ anyways thankyou for the hard work! +1 – ElektroStudios Apr 29 '13 at 03:15
  • 1
    You're weclome, it's easier to get the version as a string vs trying to get it as a decimal because of `IFormatter or IConvertable` if you can follow what I am saying.. – MethodMan Apr 29 '13 at 03:20
  • 1
    really not, I don't learned about interfaces (that sems interfaces maybe I'm wrong), but you saved me a considerable amount of time on wikipedia to assign the "OsName=" values like in your code ^^, regards! – ElektroStudios Apr 29 '13 at 03:24
1

I found the solution using VAL:

#Region " Get NT Version "

    ' [ Get NT Version Function ]
    '
    ' // By Elektro H@cker
    '
    ' Examples :
    ' MsgBox(Get_NT_Version())
    ' If Get_NT_Version() < 6.0 Then MsgBox("This application only works with an Aero compatible windows version")

    Private Function Get_NT_Version() As Double

        Dim NT As Double = CDbl(Val(System.Environment.OSVersion.Version.ToString.Substring(0, 3)))

        ' INFO:
        ' -----
        ' 3.1 = Windows NT 3.1
        ' 3.5 = Windows NT 3.5
        ' 4.0 = Windows NT 4.0
        ' 5.0 = Windows 2000
        ' 5.1 = Windows XP / Windows Fundamentals for Legacy PCs
        ' 5.2 = Windows XP 64 Bit / Windows server 2003 / Windows server 2003 R2 / Windows home Server /
        ' 6.0 = Windows VISTA / Windows server 2008
        ' 6.1 = Windows 7 / Windows server 2008 R2
        ' 6.2 = Windows 8 / Windows 8 Phone / Windows Server 2012

        Return NT

    End Function

#End Region

Simple as that!

ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
  • 1
    That will work as well I am a C# developer however I do know VB as well just think in terms of C# vs VB +1 for finding an alternative solution to your original problem – MethodMan Apr 29 '13 at 03:17