How do you read the registry value using the msbuild custom task?

I am creating an MSBuild task that will read the registry for a specific registry key. If I write the same line of code (see below) in a console application, it returns the expected result, but when it is in the MSBuild task, it does not return anything.

Return Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Setup\", "SQLPath", Nothing)

I would expect the code above to return Nothingif a key / value pair does not exist and return a value if it exists. I get Nothingwhen the MSBuild task is executed. Is there some kind of attribute that I need to apply to the Execute function of the MSBuild task to tell it that it needs to read the registry?

EDIT:

Here's what is done from the MSBuild task:

Return Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Setup\", "SQLPath", Nothing)

I believe this is caused by Registry Redirector on my Vista x64 machine running 32 bit MSBuild. Is there a way to tell the custom MSBuild task (written in VB.Net) to search in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Setup\, then only if nothing exists there, look in HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Setup\?

Thanks,

Scott Blue

+5
source share
3 answers

You can read the registry directly from MSBuild without a user task, for example:

$(registry:Hive\MyKey\MySubKey@Value)

eg.

$(registry:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Setup\)

You said you want to do this from a custom task, so this may not apply, but I'm sending it in case this helps.

+12
source

msbuild script x86 Visual Studio. 64- . , x86 64- ?

+1

How about using VB if () is a triple function?

Function GetSqlPathFromReg() As Object
    Return If(Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Setup\", "SQLPath", Nothing), _
              Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Setup\", "SQLPath", Nothing))
End Function

Assuming you have a property Output():

Private _sqlPath As String

<Output()> _
Public ReadOnly Property SqlPath() As String
    Get
        Return _sqlPath
    End Get
End Property

Then all you have to do is call it from the method Execute():

_sqlPath = GetSqlPathFromReg().ToString()
0
source

All Articles