0

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

Community
  • 1
  • 1
laruffii
  • 440
  • 1
  • 4
  • 11

4 Answers4

2

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.

Aaron Bertrand
  • 272,866
  • 37
  • 466
  • 490
1

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.

Adi Inbar
  • 12,097
  • 13
  • 56
  • 69
What have you tried
  • 11,018
  • 4
  • 31
  • 45
1

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.

SolaGratia
  • 309
  • 4
  • 18
0

This almost never fails! :) Good Luck

http://www.developerfusion.com/tools/convert/csharp-to-vb/

wayofthefuture
  • 8,339
  • 7
  • 36
  • 53