vmaglente Posted January 5, 2010 Posted January 5, 2010 Hi MSFN members. I'm planning to develop a GUI tweaking software for Windows 7 using VB .Net 2008. I need to know how to get the information to determine if the software is running on a Windows 7 operating system using VB code. Any help would be useful. Thanks in advance. Some useful informationProgramming Language: Visual Basic .Net 2008Target OS: Windows 7
MrJinje Posted January 5, 2010 Posted January 5, 2010 (edited) It is VBS from the WMI Code Creator, you may need to tweak it slightly.strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") Set colItems = objWMIService.ExecQuery( _ "SELECT * FROM SoftwareLicensingService WHERE Version = '6.1.7600.16385'",,48) For Each objItem in colItems Wscript.Echo "-----------------------------------" Wscript.Echo "SoftwareLicensingService instance" Wscript.Echo "-----------------------------------" Wscript.Echo "Version: " & objItem.VersionNext Edited January 5, 2010 by MrJinje
vmaglente Posted January 5, 2010 Author Posted January 5, 2010 Thanks for the reply Mr. Jinje. It gave me an idea how to determine the OS platform. I think I just need to find a code snippet for the VB .Net version of the code.
CoffeeFiend Posted January 5, 2010 Posted January 5, 2010 No point in trying to translate vbscript that uses WMI (it doesn't translate well at all -- normally WMI is used rather differently in .NET), especially when there's a "native" way to do it without any of that. Even if I wasn't doing it the normal way, I'd sooner PInvoke GetVersionEx. And even if I was using WMI, I still wouldn't use SoftwareLicensingService as it's only available Vista and later OS'es and as such the code (as-is) will fail (as in crash) on XP, and the Win32_OperatingSystem class is meant precisely for this.In C# you'd simply do this:System.OperatingSystem osVer = System.Environment.OSVersion;if (osVer.Version.Major == 6 && osVer.Version.Minor == 1){ //running Windows 7 or Windows 2008 R2}If my memory serves me well (haven't done any VB at all in the last couple of years), it would be translated into VB.Net like so:Dim osVer As System.OperatingSystem = System.Environment.OSVersionIf osVer.Version.Major = 6 AndAlso osVer.Version.Minor = 1 Then 'running Windows 7 or Windows 2008 R2End If
vmaglente Posted January 5, 2010 Author Posted January 5, 2010 Wow! Thanks for the reply CoffeeFiend! Question answered.
gunsmokingman Posted January 5, 2010 Posted January 5, 2010 This is a more simple way to code for VB.net the OS version If Environment.OSVersion.VersionString >= "6.1.7600" Then '-> Your Code Here MsgBox("Confirm Win 7 or better is installed") End IfOr If you want it on a single line If Environment.OSVersion.VersionString >= "6.1.7600" Then MsgBox("Confirm Win 7 or better is installed")
CoffeeFiend Posted January 5, 2010 Posted January 5, 2010 This is a more simple way to code for VB.net the OS versionSo many ways to skin a cat This one doesn't work though. And if it actually worked, it would test for Windows 7 (and 2008 R2) or later.You're taking a string made from all kinds of stuff (including the 2 simple numbers we want to check), and then use a math operator to compare the 2 different strings (I wouldn't say using greater or equal between anything else than 2 numbers "simpler" than what I had posted honestly), and it doesn't work.VersionString on Win7 is "Microsoft Windows NT 6.1.7600.0". Compare that against string any version number, and it'll return true regardless (i.e. try Environment.OSVersion.VersionString >= "9.9.9999" or such, as M is 0x4D and 9 is 0x39)Then again, one could consider checking if the string is exactly what they expect it to be but it tends to change (depending on service pack and such) so it might be error prone. Using Major and Minor will work as expected.
gunsmokingman Posted January 6, 2010 Posted January 6, 2010 You where correct on the first code I posted didnt work because of the letter in the string.Some useful informationProgramming Language: Visual Basic .Net 2008Target OS: Windows 7Since he only wants to check for Window 7 the most simple way in VB.net would be to useMy.Computer.Info.OSFullName, this produces this return on my computer Microsoft Windows 7 Ultimate Then we check to see if windows 7 is in the string.If InStr(LCase(My.Computer.Info.OSFullName), "windows 7") ThenSince he ask for VB.net and not any other language maybe you should post the correct langauge. I misreadyour first code and thought it was Vb.net sorry I am dyslexic so reading and wrighting is hard on me sometimes.Button1 Click Is set to 6.1.7600.1 the wrong version to produce the error message using >= version should be 6.1.7600.0 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If InStr(LCase(My.Computer.Info.OSFullName), "windows 7") Then Label1.Text = "Confirm " & My.Computer.Info.OSFullName Else Label1.Text = "Error " & My.Computer.Info.OSFullName End If End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If My.Computer.Info.OSVersion.ToString >= "6.1.7600.1" Then TextBox1.Text = "Confirm " & My.Computer.Info.OSFullName Else TextBox1.Text = "Error " & My.Computer.Info.OSFullName End If End SIf you want to test this with a simple vbs script using WMI and >=strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem",,48) For Each objItem in colItems If objItem.Version >= "6.1.7600" Then MsgBox "Confirm Version: " & objItem.Version,4128,"Confirm" Else MsgBox "Wrong Version",4128,"Error" End if Next
cluberti Posted January 19, 2010 Posted January 19, 2010 This wouldn't happen to be related to your homework, would it?
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now