Detect Operating System - vb.net

I would like to know how to detect if a persons operating system is Windows 7, I'm a bit new and have no idea how to do this. Please let me know if it is possible and the code to do it.

See the Environment.OSVersion property on MSDN. It is a static property that returns an OperatingSystem object, which has a Version property and you can just check the Major and Minor version numbers to see if it is 6.1 (Windows 7 is actually version 6.1).
Dim osVer As Version = Environment.OSVersion.Version
If osVer.Major = 6 And osVer.Minor = 1 Then
Console.WriteLine("win7!!")
End If

It's easy to use My.Computer.Info.OSFullName.
you need to set up the app.manifest file to get the correct version number. even System.Environment.OSVersion.ToString() ' not gives the correct version if you have not been set the app.manifest
add an app.manifest
Console.WriteLine(My.Computer.Info.OSFullName)
Console.WriteLine(My.Computer.Info.OSVersion)
Console.WriteLine(My.Computer.Info.OSPlatform)
Output:
Microsoft Windows 10 Pro
10.0.18362.0
Win32NT

I'm guessing since you're a bit new that you're actually using VB.NET rather than classic VB 6.
In VB.NET, you can use:
Dim osVersion As String = System.Environment.OSVersion.ToString()

I would use
My.Computer.Info.OSFullName

Related

Application that always worked well with MySql5xx crashes in MySql8

I'm from Brazil and this my first time here.
I've always used MySql5xx in my ERP in VB.NET using ODBC connection. It always worked very well but now I need to use the "With Recursive" clause in the query and isn't support in mySQL lower then 8.
The problem is that my app simply crashes after some few interactions with MySQL8, using the Odbc Driver, without any other messagem then just
"A problem caused your program to stop working correctly. Windows will close the program and notify you if a solution is available"
I've already tried to install mySql8 in Windows10, Srv2012, Srv2016 but the problem is always the same and I'm unabled to use it.
If someone has any tip, any sugestion, will be very welcome.
Above are the mySql8.ODBC connection strings I'm using
Dim sOdbcAnsi_MySql8_va As String = "DRIVER={MySQL ODBC 8.0 ANSI Driver};Server=DELLSRV-2016;Port=3308;DataBase=mydb;UID=root;PWD=xxxxx"
Dim sOdbcAnsi_MySql8_va As String = "DRIVER={MySQL ODBC 8.0 Unicode Driver};Server=DELLSRV-2016;Port=3308;DataBase=mydb;UID=root;PWD=xxxxx"
And here is the CNN.String that works well with mySql5xxx
Dim sOdbc_MySql51_va As String = "DRIVER={MySQL Odbc 5.1 Driver};Server=LocalHost;DataBase=madepratico_va;UID=root;PWD=1q2w3e4r"
Thanks for now

VB.Net - Use wild card in My.Computer.Registry.GetValue

I have a VB.NET script that looks up the current version of java installed.
Everything worked great until java 8 came out.
Back in Java 7 i would do this.
My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment", "Java7FamilyVersion", Nothing)
In Java 8 (Java8FamilyVersion) is gone and has replaced with (FullVersion).
The problem is FullVersion is behind two more folders one with the version (18.0_25) Then another folder call MSI
So here is the problem; right now the first folder is called 18.0_25, but in the future it would be changed to something like 18.0.55ish.
I can't update my software that often, so i would like to use a wilcard in the getvalue
IE something like this
My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\JavaSoft\Java Runtime Environment\1.8.*\MSI", "FullVersion", Nothing)
Above didn't work is their anything that would work?
Use the GetSubKeyNamesmethod to enumerate the subkey(s) of "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\JavaSoft\Java Runtime Environment", then pick the alphabetically last (so that you do not fall for any old 1.7_xx keys or the 1.8 key)
You could grab a file version from one of the Java .dll files. Sorry, I don't have Java installed, but something like this might help you:
Dim fvi As FileVersionInfo = FileVersionInfo.GetVersionInfo("somefilename.dll")
Debug.Print(fvi.ProductVersion)
You can fiddle with the returned properties for major, minor, etc. You should be able to build a version string to get what you need.

How to convert certain C# code to VB.NET

I've been googling around around and read this article
C# How to get SQL Server installation path programatically?
and this is exactly what i need in VB.NET , however i'm not good in translating this code into VB.NET Code. So, any help would greatly appreciated. Thank you.
Note : I'm using SQL Server 2005 and Visual Basic 2008
While the question was originally titled about retrieving SQL Server's installation path, I felt it was more about a code translation problem (the solution already existed, just not in the right language).
But then I thought that the method in the original code was fairly blunt.
Evan provided you with what I assume is a workable translation of the existing solution. But probably a much easier way to perform this specific task - assuming you just need to find the installation path for an instance you're already connected to, and assuming that a user who can read the registry will also have VIEW SERVER STATE permissions - is to issue this simple query against the DMV sys.dm_os_loaded_modules from your program:
SELECT name
FROM sys.dm_os_loaded_modules
WHERE name LIKE '%sqlservr.exe';
This will give you something like this:
C:\Program Files\Microsoft SQL Server\MSSQL11.SQL2012\MSSQL\Binn\sqlservr.exe
You have some parsing to do, depending on exactly what you're after (e.g. do you want to stop at MSSQL, or Binn?), but this is much easier than reading the registry or other methods that are out there IMHO.
I just used a code converter ... There are only basic things that need to be changed ..
Using sqlServerKey As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Microsoft SQL Server")
For Each subKeyName As String In sqlServerKey.GetSubKeyNames()
If subKeyName.StartsWith("MSSQL.") Then
Using instanceKey As RegistryKey = sqlServerKey.OpenSubKey(subKeyName)
Dim instanceName As String = instanceKey.GetValue("").ToString()
If instanceName = "MSSQLSERVER" Then
'say
Dim path__1 As String = instanceKey.OpenSubKey("Setup").GetValue("SQLBinRoot").ToString()
path__1 = Path.Combine(path__1, "sqlserver.exe")
Return path__1
End If
End Using
End If
Next
End Using
If you were to just read a quick article on C#, you would notice that strings are declared differently, and minor syntax discrepancies exist such as foreach vs for each
You can read here for some more common differences.
I use a very good (offline) tool, called Convert .NET Free
It's from www.fishcodelib.com
Here's a direct link to the latest release (as of 19/04/14) Size: 2.06MB, File: Zip :
[Direct Link]
Hope this is of some use ;)
P.S. This software requires .NET Framework 4.5.
This almost never fails! :) Good Luck
http://www.developerfusion.com/tools/convert/csharp-to-vb/

VB.NET read registry

Beyond perplexed this time...
The simplest possible line of code works sometimes, sometimes it doesn't. First I thought the issue was that I was trying to read the value of a DWORD, but since I CAN read DWORD values from SOME keys, that must not be the problem. Now the problem seems to be that I can't read from ANY key if the key has a space in the name. Surely this can't be. I refuse to believe that MS didn't account for spaces in registry key paths and names.
So tell me why this doesn't work:
MsgBox(My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\CA\CA ARCserve D2D\WebService", "Port", Nothing))
It just pops up an empty box. And yes, a value does exist in the registry, and yes, I have permission to read the key.
EDIT: Yup, over and over again it seems that you can't read from the registry if there are spaces anywhere in the key name. Seriously?!?
EDIT AGAIN: "Ramhound" says code examples are stupid. Fascinating point of view. However his own suggestion also failed:
Dim key As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\ATI Technologies\CBT")
Dim objValue As Object
objValue = key.GetValue("ReleaseVersion")
MsgBox(objValue.ToString())
After an entire wasted day, the solution is to set your VS project to "any cpu" in advanced compile options because if set to x86 and running on a 64bit OS you are limited to the "Wow6432node" in the registry.
It's also worth noting than on a 64 bit version of Windows 7 while running a vb.net app in 32 bit mode, the Wow6432Node key is hidden from you when using a Microsoft.Win32.RegistryKey object. I'd written this code to check which key I needed to read to get the right ODBC Driver subkey:
Dim myReg As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine
Dim myReg_Key As Microsoft.Win32.RegistryKey
myReg_Key = myReg.OpenSubKey("SOFTWARE")
strRegistry_Keys = myReg_Key.GetSubKeyNames()
bool64_Bit_OS = False
For Each strSub_Key As String In strRegistry_Keys
If strSub_Key = "Wow6432Node" Then
bool64_Bit_OS = True
End If
Next
When you do a GetSubKeyNames() on the "SOFTWARE" key you are redirected to SOFTWARE\Wow6432Node
This does make it easier for my code as now I don't need to work out which subkey to look in to find which Oracle ODBC driver to use.
Kristian

Executable directory where application is running from?

I need to get the path (not the executable) where my application is running from:
System.AppDomain.CurrentDomain.BaseDirectory()
When I run the above statement with & "/images/image.jpg" on my local machine it works fine but when I install the application on another machine it says it cannot find the file and there is a lot of extra path information some.
I just need the directory of where the app is running. I am coding in VB.NET with Visual Studio 2008.
Thanks!
This is the first post on google so I thought I'd post different ways that are available and how they compare. Unfortunately I can't figure out how to create a table here, so it's an image. The code for each is below the image using fully qualified names.
My.Application.Info.DirectoryPath
Environment.CurrentDirectory
System.Windows.Forms.Application.StartupPath
AppDomain.CurrentDomain.BaseDirectory
System.Reflection.Assembly.GetExecutingAssembly.Location
System.Reflection.Assembly.GetExecutingAssembly.CodeBase
New System.UriBuilder(System.Reflection.Assembly.GetExecutingAssembly.CodeBase)
Path.GetDirectoryName(Uri.UnescapeDataString((New System.UriBuilder(System.Reflection.Assembly.GetExecutingAssembly.CodeBase).Path)))
Uri.UnescapeDataString((New System.UriBuilder(System.Reflection.Assembly.GetExecutingAssembly.CodeBase).Path))
---
Edit October 18, 2021:
Sigh... None of the above work if using net5.0 or net6.0 and publishing app as single-file bundle. Best I got now is:
// This will give you the directory but not the assembly
string basedir = AppContext.BaseDirectory;
// Before you package the app as a single file bundle, you will get the dll.
// But after you publish it, you'll get the exe.
string pathToExecutable = Environment.GetCommandLineArgs()[0].Replace(".dll", ".exe");
Dim strPath As String = System.IO.Path.GetDirectoryName( _
System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
Taken from HOW TO: Determine the Executing Application's Path (MSDN)
I needed to know this and came here, before I remembered the Environment class.
In case anyone else had this issue, just use this: Environment.CurrentDirectory.
Example:
Dim dataDirectory As String = String.Format("{0}\Data\", Environment.CurrentDirectory)
When run from Visual Studio in debug mode yeilds:
C:\Development\solution folder\application folder\bin\debug
This is the exact behaviour I needed, and its simple and straightforward enough.
Dim P As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
P = New Uri(P).LocalPath
You could use the static StartupPath property of the Application class.
You can write the following:
Path.Combine(Path.GetParentDirectory(GetType(MyClass).Assembly.Location), "Images\image.jpg")