For today's coding tip, I will show how you can retrieve information from registry using Microsoft.Win32 namespace. This namespace provides two types of classes, those that handle events raised by the operating system and those that manipulate the system registry. Our goal is to iterate through a collection of RegistryKey objects within "LocalMachine\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" to retrieve all installed applications on Windows machine.
First we must include the following namespace.
using Microsoft.Win32;
Then to retrieve all installed applications, add the following code on your main function.
static void Main(string[] args)
{
string regkey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(regkey))
{
var query = from a in
key.GetSubKeyNames()
let r = key.OpenSubKey(a)
select new
{
Application = r.GetValue("DisplayName")
};
foreach (var item in query)
{
if (item.Application != null)
Console.WriteLine(item.Application);
}
}
}
For more LINQ coding tips & tricks, subscribe now.
Monday, July 06, 2009




0 comments:
Post a Comment