Search code examples
wixwindows-installerregistry

How to append to registry key with WiX


There's a registry key under HKML that contains a comma-separated list of drivers to load.

HKLM\Somewhere "InstalledDrivers" "foo,bar"

When installing our driver, we need to append an entry to this list so it becomes

HKLM\Somewhere "InstalledDrivers" "foo,bar,baz"

and on uninstall we need to pull just our entry away from the list, i.e. do a s/,baz// so it returns to it's original state

HKLM\Somewhere "InstalledDrivers" "foo,bar"

I can't just store the key on install and remove it on uninstall, because if other drivers get installed, the key might be "foo,bar,baz,bravo" - so it should become "foo,bar,bravo" after our driver gets uninstalled.

Oh and if ,baz is already in the list, it should not be added again.

How on earth do I wrestle WiX to manipulate keys like this?


Solution

  • There might be better ways, but using C# you could do something like this.

    Here is a C# 2019 sample:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace CommaSeparatedEditing
    {
        class Program
        {
            static void Main(string[] args)
            {
                var list = "foo, bar, baz, stack, overflow, server, fault,endingentry";
    
                List<String> Items = list.Split(',').Select(i => i.Trim()).Where(i => i != string.Empty).ToList(); // Split them all and remove spaces
                Items.Remove("fault"); // Remove any value you want
                Items.Add("My New Entry"); // Add a new entry
    
                string modifiedlist = String.Join(", ", Items.ToArray());
    
                Console.WriteLine(modifiedlist);
                Console.ReadLine(); // keep console open to show result
            }
        }
    }
    

    The above uses a typed list / generic collection from the System.Collections.Generic namespace. There are many other approaches by using more light weight string arrays, regex and replace commands that search for a specific entry and replace it with a blank space: "".

    Here are some sample snippets of some alternative ways: How do I remove comma separated specific value from a string? (scroll down the page).

    There is a WiX sample of C# deferred mode custom actions here: https://www.github.com/glytzhkof/WiXDeferredModeSample