如何使用自定义 msbuild 任务读取注册表值?

发布于 2024-08-15 10:15:33 字数 961 浏览 3 评论 0原文

我正在创建一个 MSBuild 任务,它将读取注册表中的特定注册表项。如果我在控制台应用程序中编写同一行代码(见下文),它会返回预期结果,但当它在 MSBuild 任务中时,它不会返回任何内容。

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

我希望上面的代码如果键/值对不存在则返回 Nothing ,如果存在则返回该值。当 MSBuild 任务执行时,我什么也没有得到。我是否需要将某些属性应用于 MSBuild 任务的执行函数来告诉它需要读取注册表?

编辑:

这是从 MSBuild 任务执行的内容:

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

我相信这是由 注册表重定向器,位于运行 32 位 MSBuild 的 Vista x64 计算机上。有什么方法可以告诉自定义 MSBuild 任务(用 VB .Net 编写)在 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Setup\ 中查找,然后仅当那里不存在任何内容时才在 中查找>HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Setup\

谢谢你,

斯科特·布鲁

I'm 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 within the MSBuild task, it returns nothing.

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

I would expect the above code to return Nothing if the key/value pair doesn't exist, and return the value if it does exist. I am getting Nothing when the MSBuild task gets executed. Is there some 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 is what is being executed from the MSBuild task:

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

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

Thank you,

Scott Blue

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尬尬 2024-08-22 10:15:33

您可以直接从 MSBuild 读取注册表,无需自定义任务,如下所示:

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

例如,

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

您说您希望从自定义任务中执行此操作,因此这可能不适用,但我将其发布以防万一有帮助。

You can read the registry directly from MSBuild, without a custom task, like this:

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

E.g.,

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

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

旧竹 2024-08-22 10:15:33

我们的 msbuild 脚本从 x86 Visual Studio 命令提示符运行。使用此语法时,它不会读取 64 位注册表。是否有不同的语法允许 x86 读取 64 位注册表?

Our msbuild script runs from an x86 Visual Studio Command Prompt. It does not read the 64 bit registry when using this syntax. It there a different syntax which would allow the x86 to read the 64 bit registry?

笔芯 2024-08-22 10:15:33

使用 VB 的 If() 三元函数怎么样?

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

假设您有一个 Output() 属性:

Private _sqlPath As String

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

那么您所要做的就是从 Execute() 方法调用它:

_sqlPath = GetSqlPathFromReg().ToString()

How about using VB's If() ternary 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 an Output() property:

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 calling it from the Execute() method:

_sqlPath = GetSqlPathFromReg().ToString()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文