2

我想从另一个 C# 应用程序中按其名称执行 ClickOnce 应用程序。我当前的方法从卸载注册表项 (HKCU\Software\Microsoft\Windows\CurrentVersion\Uninstall\[PublicKeyToken]\ShortcutAppId.

我们有一个存储所有 ClickOnce 应用程序的网络文件夹。ClickOnce 应用程序的每个快捷方式都以该文件夹中的 .Application 文件为目标。

当应用程序部署到 Z:\ClickOnceApps\MyApp 但从 \\server\share\ClickOnceApps\MyApp\MyApp.application 安装时,我遇到了问题。在这种情况下,存储在注册表中的 ShortcutAppId 是 Z:\ClickOnceApps\MyApps\MyApps.Application#[...] 当我尝试使用此路径启动应用程序时,它让我安装它,这让我认为实际安装路径是应用程序标识的一部分。

所以基本上我只能从安装它的同一路径启动应用程序。

我的问题:如何检索安装应用程序的精确路径(而不是部署路径)?

这是我为感兴趣的人检索 ShortcutAppId 的代码:

Public Class ClickOnceApp

        Public Property DisplayName As String
        Public Property UninstallString As String
        Public Property PublicKeyToken As String
        Public Property DisplayVersion As Version
        Public Property ShortcutAppId As String

        Public ReadOnly Property ShortcutPath As String
            Get

                Dim pawnIndex = ShortcutAppId.IndexOf("#"c)
                If pawnIndex = -1 Then
                    Return ""
                Else
                    Return ShortcutAppId.Substring(0, pawnIndex)
                End If

            End Get
        End Property

        Public Sub New(appUninstallKey As RegistryKey)

            Me.DisplayName = appUninstallKey.GetValue("DisplayName")
            Me.UninstallString = appUninstallKey.GetValue("UninstallString")
            Me.PublicKeyToken = appUninstallKey.GetValue("PublicKeyToken")
            Me.DisplayVersion = New Version(appUninstallKey.GetValue("DisplayVersion"))
            Me.ShortcutAppId = appUninstallKey.GetValue("ShortcutAppId")

        End Sub

        Public Sub Launch()
            Process.Start(ShortcutPath)
        End Sub

    End Class


    Public Class ClickOnceHelper

        Public Shared Function GetInstalledClickOnceApps() As ClickOnceApp()

            Dim list As New List(Of ClickOnceApp)()

            Using uninstallKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall")

                For Each subKeyName In (From keyName In uninstallKey.GetSubKeyNames() _
                                    Where keyName.Length = 16)

                    Using appKey = uninstallKey.OpenSubKey(subKeyName)

                        If CStr(appKey.GetValue("UninstallString")).StartsWith("rundll32.exe dfshim.dll") Then

                            Dim app = New ClickOnceApp(appKey)
                            list.Add(app)

                        End If

                    End Using

                Next

            End Using

            Return list.ToArray()

        End Function

        Public Shared Function FindClickOnceAppByName(name As String) As ClickOnceApp

            Dim list = GetInstalledClickOnceApps()
            Return (From app In list _
                    Where app.DisplayName.ToLower() = name.ToLower()).FirstOrDefault()

        End Function

        Public Shared Function FindClickOnceAppByPublicKey(publicKeyToken As String) As ClickOnceApp

            Dim list = GetInstalledClickOnceApps()
            Return (From app In list _
                    Where app.PublicKeyToken = publicKeyToken).FirstOrDefault()
        End Function

End Class
4

0 回答 0