Programmatically registering program into Add/Remove programs and storing files within an executable

Boardy picture Boardy · Aug 5, 2012 · Viewed 12.5k times · Source

I am working on a windows c# console application which I want to allow the user to install on to their computer.

I want to make my own Windows Installer executable as the Setup Deployment tools built into Visual Studio, appear to be somewhat lacking in functionality for customisations and documentation.

Therefore, because I want to make my own Windows installer, how do I register my program into the Add/Remove Programs window so they can choose to uninstall it again if they wish and it relaunches my installer program to do the removal.

Also as well, the executable would obviously need to copy the files into various locations on the PC, i.e. C:\Program Files so how would I store the executable files within the windows installer executable so I can move them into the right location.

Is this possible to do?

Thanks for any help you can provide.

Answer

Samuel Neff picture Samuel Neff · Aug 6, 2012

Here's a routine we use to register our program in Add/Remove Programs:

private void CreateUninstaller()
{
    using (RegistryKey parent = Registry.LocalMachine.OpenSubKey(
                 @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", true))
    {
        if (parent == null)
        {
            throw new Exception("Uninstall registry key not found.");
        }
        try
        {
            RegistryKey key = null;

            try
            {
                string guidText = UninstallGuid.ToString("B");
                key = parent.OpenSubKey(guidText, true) ??
                      parent.CreateSubKey(guidText);

                if (key == null)
                {
                    throw new Exception(String.Format("Unable to create uninstaller '{0}\\{1}'", UninstallRegKeyPath, guidText));
                }

                Assembly asm = GetType().Assembly;
                Version v = asm.GetName().Version;
                string exe = "\"" + asm.CodeBase.Substring(8).Replace("/", "\\\\") + "\"";

                key.SetValue("DisplayName", "My Program");
                key.SetValue("ApplicationVersion", v.ToString());
                key.SetValue("Publisher", "My Company");
                key.SetValue("DisplayIcon", exe);
                key.SetValue("DisplayVersion", v.ToString(2));
                key.SetValue("URLInfoAbout", "http://www.blinemedical.com");
                key.SetValue("Contact", "[email protected]");
                key.SetValue("InstallDate", DateTime.Now.ToString("yyyyMMdd"));
                key.SetValue("UninstallString", exe + " /uninstallprompt");
            }
            finally
            {
                if (key != null)
                {
                    key.Close();
                }
            }
        }
        catch (Exception ex)
        {
            throw new Exception(
                "An error occurred writing uninstall information to the registry.  The service is fully installed but can only be uninstalled manually through the command line.",
                ex);
        }
    }
}