Sei sulla pagina 1di 4

Instalador.

vb
Imports
Imports
Imports
Imports
Imports

Pgina 1

System.Deployment.Application
System.Reflection
System.IO
Microsoft.Win32
System.Security.Policy

Public Class Instalador


Public Shared Sub ConfigurarIcono()
Try
If ApplicationDeployment.IsNetworkDeployed And ApplicationDeployment.Cur
rentDeployment.IsFirstRun Then
Dim codigo = Assembly.GetExecutingAssembly()
Dim descripcion = String.Empty
If (Attribute.IsDefined(codigo, GetType(AssemblyDescriptionAttribute
))) Then
Dim ensamb = CType(Attribute.GetCustomAttribute(codigo, GetType(
AssemblyDescriptionAttribute)), AssemblyDescriptionAttribute)
descripcion = ensamb.Description
End If
Dim rutaIcono = Path.Combine(Application.StartupPath, "icongyu.ico")
If Not File.Exists(rutaIcono) Then
Return
End If
Dim llavesDesinstalacion = Registry.CurrentUser.OpenSubKey("Software
\\Microsoft\\Windows\\CurrentVersion\\Uninstall")
Dim llaves = llavesDesinstalacion.GetSubKeyNames().ToList()
For Each clave As String In llaves
Dim llave = llavesDesinstalacion.OpenSubKey(clave, True)
Dim nombre = llave.GetValue("DisplayName")
If Not nombre Is Nothing And nombre.ToString().Equals(descripcio
n) Then
llave.SetValue("DisplayIcon", rutaIcono)
Exit For
End If
Next
End If
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
End Sub
'VB
''' <summary>
''' Uninstall the current version of the application.
''' </summary>
Public Shared Sub UninstallMe()
Dim publicKeyToken As String = GetPublicKeyToken()
'Find Uninstall string in registry
Dim DisplayName As String = Nothing
Dim uninstallString As String = _
GetUninstallString(publicKeyToken, DisplayName)
If (uninstallString.Length <= 0) Then
Return
End If
Dim runDLL32 As String = uninstallString.Substring(0, 12)
Dim args As String = uninstallString.Substring(13)
'start the uninstall; this will bring up the uninstall dialog
' asking if it's ok
Dim uninstallProcess As Process = Process.Start(runDLL32, args)
'push the OK button
PushUninstallOKButton(DisplayName)
End Sub
'VB
''' <summary>
''' Gets the public key token for the current running

Instalador.vb

Pgina 2

''' (ClickOnce) application.


''' </summary>
''' <returns></returns>
Public Shared Function GetPublicKeyToken() As String
Dim asi As ApplicationSecurityInfo = _
New ApplicationSecurityInfo(AppDomain.CurrentDomain.ActivationContext)
Dim pk As Byte() = asi.ApplicationId.PublicKeyToken
Dim pkt As StringBuilder = New StringBuilder()
For i As Integer = 0 To pk.GetLength(0) - 1 Step 1
pkt.Append(String.Format("{0:x2}", pk(i)))
Next
Return pkt.ToString()
End Function
'VB
''' <summary>
''' Gets the uninstall string for the current ClickOnce app
''' from the Windows Registry.
''' </summary>
''' <param name="PublicKeyToken">The public key token of the app.</param>
''' <returns>The command line to execute
''' that will uninstall the app.</returns>
Public Shared Function GetUninstallString(ByVal PublicKeyToken As String, _
ByRef DisplayName As String) As String
Dim uninstallString As String = Nothing
'set up the string to search for
Dim searchString As String = "PublicKeyToken=" & PublicKeyToken
'open the registry key and get the subkey names
Dim uninstallKey As RegistryKey = _
Registry.CurrentUser.OpenSubKey("Software\Microsoft\" & _
"Windows\CurrentVersion\Uninstall")
Dim appKeyNames As String() = uninstallKey.GetSubKeyNames()
DisplayName = Nothing
Dim found As Boolean = False
''search through the list for one with a match
For Each appKeyName As String In appKeyNames
Dim appKey As RegistryKey = uninstallKey.OpenSubKey(appKeyName)
uninstallString = appKey.GetValue("UninstallString").ToString()
DisplayName = appKey.GetValue("DisplayName").ToString
appKey.Close()
'look for the public key token, and the display name
'(same as ProductName in the ClickOnce properties)
If (uninstallString.Contains(PublicKeyToken) _
AndAlso DisplayName = "TestCertExp_VB") Then
found = True
Exit For
End If
Next
uninstallKey.Close()
If (found) Then
Return uninstallString
Else
Return String.Empty
End If
End Function
'VB
''' <summary>
''' Find and Push the OK button on the uninstall dialog.

Instalador.vb

Pgina 3

''' </summary>
''' <param name="DisplayName">Display Name value from the registry</param>
Private Shared Sub PushUninstallOKButton(ByVal DisplayName As String)
Dim success As Boolean = False
''Find the uninstall dialog.
Dim uninstallerWin As IntPtr = _
FindUninstallerWindow(DisplayName, success)
Dim OKButton As IntPtr = IntPtr.Zero
''If it found the window, look for the button.
If (success) Then
OKButton = FindUninstallerOKButton(uninstallerWin, success)
End If
''If it found the button, press it.
If (success) Then
DeploymentUtilsWin32.DoButtonClick(OKButton)
End If
End Sub
'VB
''' <summary>
''' Find the uninstall dialog.
''' </summary>
''' <param name="DisplayName">Display Name retrieved
''' from the registry.</param>
''' <param name="success">Whether the window was found or not.</param>
''' <returns>Pointer to the uninstall dialog.</returns>
Private Shared Function FindUninstallerWindow(ByVal DisplayName As String, _
ByRef success As Boolean) As IntPtr
''Max number of times to look for the window,
''used to let you out if there's a problem.
Dim i As Integer = 25
Dim w32 As New DeploymentUtilsWin32()
Dim uninstallerWindow As IntPtr = IntPtr.Zero
Do While (uninstallerWindow = IntPtr.Zero AndAlso i > 0)
uninstallerWindow = _
w32.SearchForTopLevelWindow(DisplayName + " Maintenance")
System.Threading.Thread.Sleep(500)
i -= 1
Loop
If (uninstallerWindow = IntPtr.Zero) Then
success = False
Else
success = True
End If
Return uninstallerWindow
End Function
'VB
''' <summary>
''' Find the OK button on the uninstall dialog.
''' </summary>
''' <param name="UninstallerWindow">The pointer to
''' the Uninstall Dialog</param>
''' <param name="success">Whether it succeeded or not.</param>
''' <returns>A pointer to the OK button</returns>
Private Shared Function FindUninstallerOKButton(ByVal UninstallerWindow _
As IntPtr, ByRef success As Boolean) As IntPtr
''max number of times to look for the button,
''lets you out if there's a problem
Dim i As Integer = 25
Dim w32 As New DeploymentUtilsWin32()
Dim OKButton As IntPtr = IntPtr.Zero
Do While (OKButton = IntPtr.Zero AndAlso i > 0)
OKButton = w32.SearchForChildWindow(UninstallerWindow, "&OK")

Instalador.vb
System.Threading.Thread.Sleep(500)
i -= 1
Loop
If (OKButton = IntPtr.Zero) Then
success = False
Else
success = True
End If
Return OKButton
End Function
'VB
''' <summary>
''' Install the new version of the application from a different URL.
''' Then exit this version of the application.
''' Trigger this specifically with Internet Explorer
'''
in case the user has their default browser set to something else.
''' This eliminates any problems running it with Firefox.
''' If you are changing something like the target CPU, or installing
'''
a new prerequisite, run setup.exe.
''' Otherwise, you can just call the application fle directly,
'''
because the user won't have the application installed w/o the
'''
current prerequisites.
''' </summary>
Public Shared Sub InstallNewVersion()
Dim url As String = _
"http://localhost/NewVersion/TestCertExp_VB.application"
System.Diagnostics.Process.Start("iexplore.exe", url)
System.Windows.Forms.Application.Exit()
Return
End Sub
End Class

Pgina 4

Potrebbero piacerti anche