Reputation: 1290
My goal is to take any pending Windows updates that are not explicitly called out in an exception list and dump them into a secondary UpdateCollection of updates that should be installed. My pseudo code looks like this:
String list containing KB numbers for updates that shouldn't be installed:
List<string> windowsUpdateExceptionKBList
KB1234567
KB2644615
KB483729
WUApiLib.UpdateCollection containing update objects
UpdateCollection securityUpdatesList
Iupdate object1
Iupdate object2
Each update object has a ".Title" property containing the KB number (e.g. Security Update for Windows 7 for x64-based Systems (KB2644615)). I need something like the following:
UpdateCollection securityUpdatestoInstall = new UpdateCollection();
foreach (Iupdate update in securityUpdatesList)
{
foreach (string kB in windowsUpdateExceptionKBList)
{
if (!update.Title **contains** kB)
{
securityUpdatestoInstall.Add(update);
}
}
}
The above won't work as proposed because it will add duplicate updates to the list due to the nested foreach loops. But I'm having a hard time even conceptualizing how I can accomplish this in C#.
Upvotes: 0
Views: 101
Reputation: 12978
You need to take action if no match was found, so add a variable to track whether a match was found, and break out of the inner foreach
on the first match, then conditionally add the update depending if a match was found:
UpdateCollection securityUpdatestoInstall = new UpdateCollection();
foreach (Iupdate update in securityUpdatesList)
{
bool blacklisted = false;
foreach (string kB in windowsUpdateExceptionKBList)
{
if (update.Title.Contains(kB))
{
blacklisted = true;
break;
}
}
if (!blacklisted)
{
securityUpdatestoInstall.Add(update);
}
}
Also note that you can use string.Contains, so I've added that too (and notice that it's case sensitive).
Upvotes: 1