How to add my program to add/remove programs? VB.NET

4.1k views Asked by At

I am creating a vb.net installation in winforms, and I want that my app will be added, like in other programs, to Control Panel -> Programs -> Programs and Features and Features with it's info.

I tried to do it with the following code:

My.Computer.Registry.LocalMachine.CreateSubKey("Software\Microsoft\Windows\CurrentVersion\Uninstall\Myapp").SetValue("Display Name", "Appname")
My.Computer.Registry.LocalMachine.CreateSubKey("Software\Microsoft\Windows\CurrentVersion\Uninstall\Myapp").SetValue("Display Version", "1.0.0.0")
My.Computer.Registry.LocalMachine.CreateSubKey("Software\Microsoft\Windows\CurrentVersion\Uninstall\Myapp").SetValue("Publisher", "SomePublisher")
My.Computer.Registry.LocalMachine.CreateSubKey("Software\Microsoft\Windows\CurrentVersion\Uninstall\Myapp").SetValue("Uninstall Path", "uninstpath")

What's wrong in my code, and how can I fix it?

2

There are 2 answers

0
Benjli On BEST ANSWER

I found an indirect way to do it - via cmd. That's not the best way, but it may work.

You can write the following text in the resources as name.cmd:

'sets the name of your app to display
reg add "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\Yourapp" /v "DisplayName" /d "Yourapp" /f 
'sets your app version to display
REG ADD "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\Yourapp" /V "DisplayVersion" /D "1.0.0.0"
'sets your app icon to display location
REG ADD "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\Yourapp" /V "DisplayIcon" /D "%PROGRAMFILES%\Yourapp\yourapp.ico"
'sets your name to display as publisher
REG ADD "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\Yourapp" /V "Publisher" /D "Yourname"
'sets the uninstall path
REG ADD "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\Yourapp" /V "UninstallString" /D "%PROGRAMFILES%\Yourapp\UNINSTALL.exe"
REG ADD "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\Yourapp" /V "UninstallPath" /D "%PROGRAMFILES%\Yourapp\UNINSTALL.exe"
' sets the install directory
REG ADD "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\Yourapp" /V "InstallLocation" /D "%PROGRAMFILES%\Yourapp\"

And then, you open it in cmd programically:

Dim procInfo As New ProcessStartInfo()

procInfo.UseShellExecute = True
'writes the file name.cmd to appdata
IO.File.WriteAllText(My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData + "/name.cmd", My.Resources.name)
'sets the path of the file to be opened to name.cmd
procInfo.FileName = My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData + "/name.cmd"

procInfo.WorkingDirectory = ""
procInfo.Verb = "runas"
'running the file in cmd
Process.Start(procInfo)
0
Phillip On

Here is a way without using a cmd, shell or batch file:

First of all your Setup-Application needs to be permitted to write in the Registry. You can achive that by setting your "requestedExecutionLevel" to "highestAvailable":

in file: "app.manifest"

<requestedExecutionLevel level="highestAvailable" uiAccess="false" />

Now you can use your DotNetFramework functions to create you uninstall RegistryKey:

        'Setting My Values
        Dim ApplicationName As String = "Yourapp"
        Dim ApplicationVersion As String = "1.0.0.0"
        Dim ApplicationIconPath As String = "%PROGRAMFILES%\Yourapp\yourapp.ico"
        Dim ApplicationPublisher As String = "Yourname"
        Dim ApplicationUnInstallPath As String = "%PROGRAMFILES%\Yourapp\UNINSTALL.exe"
        Dim ApplicationInstallDirectory As String = "%PROGRAMFILES%\Yourapp\"

        'Openeing the Uninstall RegistryKey (don't forget to set the writable flag to true)
        With My.Computer.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Uninstall", True)

            'Creating my AppRegistryKey
            Dim AppKey As Microsoft.Win32.RegistryKey = .CreateSubKey(ApplicationName)

            'Adding my values to my AppRegistryKey
            AppKey.SetValue("DisplayName", ApplicationName, Microsoft.Win32.RegistryValueKind.String)
            AppKey.SetValue("DisplayVersion", ApplicationVersion, Microsoft.Win32.RegistryValueKind.String)
            AppKey.SetValue("DisplayIcon", ApplicationIconPath, Microsoft.Win32.RegistryValueKind.String)
            AppKey.SetValue("Publisher", ApplicationPublisher, Microsoft.Win32.RegistryValueKind.String)
            AppKey.SetValue("UninstallString", ApplicationUnInstallPath, Microsoft.Win32.RegistryValueKind.String)
            AppKey.SetValue("UninstallPath", ApplicationUnInstallPath, Microsoft.Win32.RegistryValueKind.String)
            AppKey.SetValue("InstallLocation", ApplicationInstallDirectory, Microsoft.Win32.RegistryValueKind.String)

        End With