a blog for those who code

Friday, 6 January 2017

Detect Antivirus is installed in a System using C#

In this post we will be detecting if any antivirus is been installed in a system using C# code. There are time where your application want to know that is if any antivirus is being installed in the client system for not, so this code will help you detect and get the antivirus name.

For example you are building a Antivirus, and through your code you want to notify users that there is already an antivirus installed in the users system. This will be useful because user might not want to install ore than one antivirus in the system. This is one of the scenario where you want to know if the antivirus is installed or not, but there can be more such examples.

In Windows, you have Windows Management Instrument (WMI) from where you can get the information about the antivirus installed. In C# we need to search for "Antivirus Product" using ManagementObjectSearcher class as shown below :


We will be using public ManagementObjectSearcher(string scope, string queryString); where we will pass the scope in which to query and the query to be invoked.

Full Code :

using System;
using System.Management;

class Program
{
 static void Main(string[] args)
 {
 // We are searching for Antivirus Product in Security Center
  using (var antiVirusSearch = new ManagementObjectSearcher(@"\\" + Environment.MachineName + @"\root\SecurityCenter2", "Select * from AntivirusProduct"))
  {
   var getSearchResult = antiVirusSearch.Get();
   foreach (var searchResult in getSearchResult)
   {
    Console.WriteLine("Antivirus Name : " + searchResult["displayName"].ToString());
    Console.WriteLine("Path : " + searchResult["pathToSignedProductExe"].ToString());
    }
   }
   Console.Read();
  }
}

Result :


Please Like and Share the CodingDefined Blog, if you find it interesting and helpful.

No comments:

Post a Comment