SDK: How to add an enhanced detection method into an application deployment type

Update #1: I have attached the sample as a .cs file to make it easier to consume

In my previous post I provided a simple sample on how to create an application with a very basic detection method. If you want to perform more advanced detection, you can leverage the desired configuration management (DCM) SDK to create an extremely rich set of conditions called enhanced detection methods (EHD).

These EHD classes require the DcmObjectModel.dll assembly which is located with the administrator console.

The sample here will demonstrate creating an EHD and adding it to a deployment type. Use the sample program in this post and replace the code between lines 34 and 35 with the code that follows:

Code Snippet

  1. installer.DetectionMethod = DetectionMethod.Enhanced;
  2. EnhancedDetectionMethod ehd = new EnhancedDetectionMethod();
  3.  
  4. // Get value from this registry setting
  5. RegistrySetting registrySetting = new RegistrySetting(null);
  6. registrySetting.RootKey = RegistryRootKey.LocalMachine;
  7. registrySetting.Key = @"SOFTWARE\MyCompany\MyProduct";
  8. registrySetting.Is64Bit = true;
  9. registrySetting.ValueName = "IsInstalled";
  10. registrySetting.CreateMissingPath = false;
  11. registrySetting.SettingDataType = DataType.Int64;
  12.  
  13. // Add the setting to the EHD
  14. ehd.Settings.Add(registrySetting);            
  15.  
  16. // The expected value of the detected registry key
  17. ConstantValue constValue = new ConstantValue("1", DataType.Int64);
  18.  
  19. // Create the reference to the EHD setting
  20. SettingReference settingRef = new SettingReference(
  21.     application.Scope,
  22.     application.Name,
  23.     application.Version.GetValueOrDefault(),
  24.     registrySetting.LogicalName,
  25.     registrySetting.SettingDataType,
  26.     registrySetting.SourceType,
  27.     false);
  28.  
  29. settingRef.MethodType = ConfigurationItemSettingMethodType.Value;
  30.  
  31. // Create a collection of operands, these will be compared in an Expression
  32. CustomCollection<ExpressionBase> operands = new CustomCollection<ExpressionBase>();
  33. operands.Add(settingRef);
  34. operands.Add(constValue);
  35.  
  36. // Expression to verify that all of the operands meet the ExpressionOperator value            
  37. Expression exp = new Expression(ExpressionOperator.IsEquals, operands);
  38.  
  39. // Create the rule
  40. Rule rule = new Rule("MyRuleId", NoncomplianceSeverity.None, null, exp);
  41.  
  42. // Now create the deployment type and bind it to the installer
  43. DeploymentType scriptDT = new DeploymentType(installer, ScriptInstaller.TechnologyId, NativeHostingTechnology.TechnologyId);
  44. scriptDT.Title = "My Application Installer";
  45.  
  46. ehd.Rule = rule;
  47.  
  48. // Now add the rule to the detection method.
  49. installer.EnhancedDetectionMethod = ehd;

This will show in the administrator console like this:

image

This is just one example of the kind of powerful detection you can perform with EHDs.

Similar code can be used for creating requirement rules as well (these are added into your DeploymentType.Rules collection).

Program.cs