Click once is a great way to install win form applications within your organization. One issue I discovered recently is that the installed application’s icon does not appear in the Add/Remove programs (or Program and Features) dialog screen. This does not look very professional.
Searching the internet I found a work around here (this has both C# and VB.Net examples):
https://goo.gl/MpjXev
I made a few changes to the VB.Net code which I share below. A few issues I found that you should keep in mind:
- Icon file should be set to Copy Always for the property Copy to Output Directory
- Make sure your display name and product name match exactly as these are compared.
- Icon is only updated on the first run of the application after install, so if you’re testing this code you will need to un-install before the next test/install.
Here’s my code, you will need to replace the calendar.ico text with your icon file and add the imports.
Imports System.Deployment.Application Imports Microsoft.Win32 Sub SetAddRemoveProgramsIcon() Try 'only run if deployed If ApplicationDeployment.IsNetworkDeployed And ApplicationDeployment.CurrentDeployment.IsFirstRun Then Dim iconSourcePath As String = System.IO.Path.Combine(Application.StartupPath, "calendar.ico") If Not File.Exists(iconSourcePath) Then Return End If Dim myUninstallKey As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Uninstall") Dim mySubKeyNames As String() = myUninstallKey.GetSubKeyNames() Dim i As Integer For i = 0 To mySubKeyNames.Length Dim myKey As RegistryKey = myUninstallKey.OpenSubKey(mySubKeyNames(i), True) Dim registryAppName As Object = myKey.GetValue("DisplayName") Dim registryAppVersion As Object = myKey.GetValue("DisplayVersion") If registryAppName <> Nothing And registryAppVersion <> Nothing And registryAppName.ToString().StartsWith(Application.ProductName) And registryAppVersion = ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString() Then myKey.SetValue("DisplayIcon", iconSourcePath) Exit For End If Next End If Catch ex As Exception Throw End Try End Sub
Let me know if you have any questions.
Cheers,
Wade
3 thoughts on “Adding a Missing Icon to Program and Features for a Click Once install, VB.NET”
I had no idea how to approach this befnoe-orw I’m locked and loaded.
Thanks for the code. I cleaned it up in VS2017 with help from JetBrains. Thought I would share my changes back to you.
Private Shared Sub SetAddRemoveProgramsIcon()
‘only run if deployed
If ApplicationDeployment.IsNetworkDeployed And ApplicationDeployment.CurrentDeployment.IsFirstRun Then
Dim iconSourcePath As String = Path.Combine(Application.StartupPath, “cet.ico”)
If Not File.Exists(iconSourcePath) Then Return
Dim myUninstallKey As RegistryKey = Registry.CurrentUser.OpenSubKey(“Software\Microsoft\Windows\CurrentVersion\Uninstall”)
Dim mySubKeyNames As String() = myUninstallKey.GetSubKeyNames()
For Each subKeyName In mySubKeyNames
Dim myKey As RegistryKey = myUninstallKey.OpenSubKey(subKeyName, True)
Dim registryAppName As String = myKey.GetValue(“DisplayName”).ToString
Dim registryAppVersion As String = myKey.GetValue(“DisplayVersion”).ToString
If Not String.IsNullOrEmpty(registryAppName) And Not String.IsNullOrEmpty(registryAppVersion) And
registryAppName.StartsWith(Application.ProductName) And
registryAppVersion = ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString() Then
‘ If we have finally located the correct key we will attempt to set the icon
Try
myKey.SetValue(“DisplayIcon”, iconSourcePath)
Catch e As Exception
‘ Do something about the exception or throw it back
Throw
End Try
Exit For
End If
Next subKeyName
End If
End Sub
Thanks, I’ll have a look this weekend and update my code with your changes.