50% OFF!!!

Thursday, December 2, 2010

C# | check if program/application is installed on remote computer

As an advance to my previous post about checking if a program or an application is installed on my comptuer, I bringing a code for doing this validation on a remote computer.

The code is the same as checking for installed app in local comptuer (as described here) except that checking remote machine registry.
The remote machine registry checking is preformed using the static method OpenRemoteBaseKey of the class RegistryKey.

The code:
public static bool IsAppInstalled(string p_machineName, string p_name)
{
   string keyName;
 
   // search in: CurrentUser
   keyName = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
   if (ExistsInRemoteSubKey(p_machineName, RegistryHive.CurrentUser, keyName, "DisplayName", p_name) == true)
   {
      return true;
   }
 
   // search in: LocalMachine_32
   keyName = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
   if (ExistsInRemoteSubKey(p_machineName, RegistryHive.LocalMachine, keyName, "DisplayName", p_name) == true)
   {
      return true;
   }
 
   // search in: LocalMachine_64
   keyName = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
   if (ExistsInRemoteSubKey(p_machineName, RegistryHive.LocalMachine, keyName, "DisplayName", p_name) == true)
   {
      return true;
   }
 
   return false;
}
private static bool ExistsInRemoteSubKey(string p_machineName, RegistryHive p_hive, string p_subKeyName, string p_attributeName, string p_name)
{
   RegistryKey subkey;
   string displayName;
 
   using (RegistryKey regHive = RegistryKey.OpenRemoteBaseKey(p_hive, p_machineName))
   {
      using (RegistryKey regKey = regHive.OpenSubKey(p_subKeyName))
      {
         if (regKey != null)
         {
            foreach (string kn in regKey.GetSubKeyNames())
            {
               using (subkey = regKey.OpenSubKey(kn))
               {
                  displayName = subkey.GetValue(p_attributeName) as string;
                  if (p_name.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true) // key found!
                  {
                     return true;
                  }
               }
            }
         }
      }
   }
   return false;
}
    

How to use this code:
string MACHINE_NAME = "MY-PC-NAME";
string APPLICATION_NAME = "APP-NAME";
try
{
bool isAppInstalled = IsAppInstalled(MACHINE_NAME, APPLICATION_NAME);
  string msg = string.Format("Application '{0}' is {1} on the remote-machine '{2}'!",
APPLICATION_NAME,
isAppInstalled ? "installed" : "NOT installed",
MACHINE_NAME);
MessageBox.Show(msg);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}



Enjoy...
MDB-Blog

3 comments:

  1. Is there any way to install a application / Wisdows Service in remote machine

    ReplyDelete
  2. I successfully execute the program, however i am getting "requested registry access is not allowed". for some single word installed software I was able to get correct result

    ReplyDelete
  3. If you don't have administrator access to remote machine, the mentioned error will occur.

    ReplyDelete