C# Linq Registry values to DataTable
<p>I am trying to enumerate the registry to fetch a list of installed applications and return them via Linq to a DataTable in C#.</p>

<p>I have tried various things including sanitizing it as XML (which worked), however it seemed rather inefficient as ultimately, i require this as DataTable object.</p>

<p>Here is where i am at currently:</p>

<pre><code>            //Warning 
   public static DataRow DataRowInstalledApplication (this RegistryKey rgkey, string keyName)
        {
            RegistryKey key = rgkey.OpenSubKey(keyName, false);
            try
            {
                //Application Name is mandetory for a given key.
                if (key == null|| key.RegToString("DisplayName", false) == null )return null;
                //Build a sanitised data row
                var rowBuilder = new DataTable().NewRow();
                rowBuilder["DisplayName"] = key.RegToString("DisplayName");
                rowBuilder["UninstallString"] = key.RegToString("UninstallString");
                rowBuilder["InstallLocation"] = key.RegToString("InstallLocation");
                rowBuilder["Publisher"] = key.RegToString("Publisher");
                rowBuilder["DisplayIcon"] = key.RegToString("DisplayIcon");

                return rowBuilder;
            }
            finally
            {
                if (key != null) key.Close();
            }

        }
</code></pre>

<p>Here is the method that contains the Linq:</p>

<pre><code>        public DataTable GetRegistryApplicationDataTable(RegistryKey registryKey, string tableName)
    {

        if (registryKey != null)
        {
            try
            {
                //change to throw non critical error
                var installedListXml = new DataTable(tableName, from name in registryKey.GetSubKeyNames()
                                                                let app = registryKey.DataRowInstalledApplication(name)

                                                                select app);
                return installedListXml;
            }
            catch
            {
                return new DataTable(tableName);
            }
            finally
            {
                registryKey.Close();
            }

        }
        return null;
    }
</code></pre>

<p>The main problem i have is that i do not really understand Linq very well. Mainly how individual values are used during the iteration to call other things similar to</p>

<pre><code>foreach (string value in collection)
{
    Somefunction(value);
}
</code></pre>

<p>, in the case of the registry values i am trying to retrieve, i do not understand how to make a Linq query pass each key name to function, which will generate a row and the Linq Query return as a data table.</p>

<p>I would be grateful of any pointers!  thanks</p>

<p>ps i call the above with</p>

<pre><code>private static readonly RegistryKey HKLMUninstallKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");

GetRegistryApplicationDataTable(HKLMUninstallKey, "Computer")
</code></pre>

<p>and expect back a data table called computer.</p>