C# - Determine antivirus program

By , 9/17/2012
(0 ratings)
This C# snippet finds a lot of information about the currently installed antivirus program. Alternatively, it can also detect the active firewall or anti-spyware protection.

* The function GetAntiVirusInfoString() (recommended) returns a string with information about the currently installed antivirus program, formated as follows: $name: $value and seperated by linebreaks.
* The function GetAntiVirusInfo() returns a NameValueCollection with information about the currently installed antivirus program.
You can choose to determine AntiVirusProduct, AntiSpywareProduct or FirewallProduct by changing the parameter product.

If you use Windows XP you will probably have to replace "root\\SecurityCenter2" with "root\\SecurityCenter".

Example:
MessageBox.Show(GetAntiVirusInfoString());

Author: marioka, translation by Michael List
// Required using directives
// If necessary, include System.Management.dll too
using System;
using System.Collections.Specialized;
using System.Management;

/// <summary>
/// Returns a string that contains information about the
/// currently installed antivirus program. The string is formated as follows: $name: $value
/// </summary>
/// <param name="product">Specification of the class to be looked into
/// [AntiVirusProduct, AntiSpywareProduct, FirewallProduct]</param>
/// <returns>String with the found information</returns>
public static string GetAntiVirusInfoString(string product = "AntiVirusProduct")
{
    // Get NameValueCollection
    NameValueCollection avCollection = GetAntiVirusInfo(product);

    // String, where the output is written into
    String output = String.Empty;

    foreach (string key in avCollection.AllKeys)
    {
        // Append key-value pairs to the output string (in scheme $name: $value)
        output += key + ": " + avCollection[key] + Environment.NewLine;
    }

    return output;
}

/// <summary>
/// Returns a NameValueCollection that contains information
/// about the currently installed antivirus product
/// </summary>
/// <param name="product">Specification of the class to be looked into
/// [AntiVirusProduct, AntiSpywareProduct, FirewallProduct]</param>
/// <returns>NameValueCollection that contains the found information</returns>
public static NameValueCollection GetAntiVirusInfo(string product = "AntiVirusProduct")
{
    // Looks for the wanted class in the WMI namespace
    // (applicable for AntiVirusProduct, AntiSpywareProduct, FirewallProduct)
    // Using Windows XP systems you have to replace SecurityCenter2 with SecurityCenter.
    ManagementObjectSearcher objSearcher =
        new ManagementObjectSearcher("root\\SecurityCenter2", "SELECT * FROM " + product);

    // NameValueCollection where the result should be saved
    NameValueCollection outputCollection = new NameValueCollection();

    foreach (ManagementObject queryObj in objSearcher.Get())
    {
        foreach (PropertyData propertyData in queryObj.Properties)
        {
            // Add found properties to the collection
            outputCollection.Add(propertyData.Name.ToString(), propertyData.Value.ToString());
        }
    }

    return outputCollection;
}

Comments

 

Log in, to comment!