Thursday 16 December 2010

How to get list of Windows 7 Startup programs from Registry using C#?

I was googling and digging into windows Startup apps running from registry keys and the whole startup order proccess. Here is what I found out.


Startup apps order list. 
(I am using Windows 7 HomePremium 64 bit.)


  • Boot execute HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\BootExecute
  • Run Services

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunServicesOnce
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunServices

  • Logon Prompt - user enter username and password
  • UserInit HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\UserInit
  • Start Shell HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\Shell
  • LocalMachine Run keys

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnceEx



  • LocalMachine Run keys - for 32 bit apps running on 64bit OS

HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\RunOnce
HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Run
HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\RunOnceEx


    • CurrentUser Run keys

    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce
    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnceEx

    • StartUp Folder
    I checked my Registry only Run SubKeys are actually holding startup apps and location paths. So to get all running apps its enough to get their string values and to run links in StartUp folder.


    public void GetAllApps()
    {

    Microsoft.Win32.RegistryKey key;
    key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", false);
    //MessageBox.Show(key.ValueCount.ToString());
    foreach (string appName in key.GetValueNames())
    {
    try
    {

    // MessageBox.Show((string)key.GetValue(appName));
    ParsePath(appName, (string)key.GetValue(appName),ref data);
    db.Insert("startup", data);
    }
    catch (Exception ex)
    {

    }

    }
    key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run", false);

    foreach (string appName in key.GetValueNames())
    {
    try
    {
    MessageBox.Show((string)key.GetValue(appName));
    }
    catch (Exception ex)
    {

    }

    }
    key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", false);
    foreach (string appName in key.GetValueNames())
    {
    try
    {
     MessageBox.Show((string)key.GetValue(appName));
    }
    catch (Exception ex)
    {

    }

    }

    DirectoryInfo di = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Startup));
    FileInfo[] links = di.GetFiles("*.lnk");
    foreach (FileInfo fi in links)
    {
    MessageBox.Show(fi.FullName); 

    }
    key.Close();

    }