Activate a Biztalk Receive Location using Code

With this code yo can (re)activate a Biztalk Server Receive Location with e.g. a surveillance service. This may make sense if you use a POP3 Receive Location where the pop3 server casually is not reachable.

System.Management.ManagementObject requiredRLObj = null;
System.Management.ObjectGetOptions objGetOptions = new ObjectGetOptions();
System.Management.ManagementClass rlObjectClass = new ManagementClass("root\\MicrosoftBizTalkServer", "MSBTS_ReceiveLocation", objGetOptions);

System.Management.EnumerationOptions enumObjOptions = new EnumerationOptions();
enumObjOptions.ReturnImmediately = false;
System.Management.ManagementObjectCollection rlObjColl = rlObjectClass.GetInstances(enumObjOptions);

foreach (ManagementObject rlObjInstance in rlObjColl)
{
if (rlObjInstance["Name"] != null)
{
if (string.Compare("ReceiveLocationName", rlObjInstance["Name"].ToString(), true, System.Globalization.CultureInfo.CurrentUICulture) == 0)
{
requiredRLObj = rlObjInstance;
}
}
}

try
{
requiredRLObj.InvokeMethod("Disable", null);
requiredRLObj.InvokeMethod("Enable", null);
}
catch
{
Console.Write("Enabling Receive Location failed");
}