如何从我的C#程序中获取我的系统中安装的防病毒的路径?

时间:2014-11-27 09:59:29

标签: c# .net

我的C#程序需要打开我机器上安装的防病毒软件。

截至目前,我已按如下方式对路径进行了硬编码:

System.Diagnostics.Process.Start("C:/Program Files (x86)/MyAntivirus/myAntivirus.exe");

然而,32和64位机器的路径会有所不同。我无法在64位Windows 8.1机器上运行相同的代码。

有没有办法在我的机器上安装防病毒软件的路径,以便我的程序与机器无关?

2 个答案:

答案 0 :(得分:2)

您可以询问路径所在的窗口,而不是硬编码防病毒的路径。大多数防病毒程序都会向Windows报告。因此Windows不会向用户报告没有安装防病毒软件。

使用WMI,您可以查询该路径的窗口。

var searcherPreVista = new ManagementObjectSearcher(string.Format(@"\\{0}\root\SecurityCenter", Environment.MachineName), "SELECT * FROM AntivirusProduct");
var searcherPostVista = new ManagementObjectSearcher(string.Format(@"\\{0}\root\SecurityCenter2", Environment.MachineName), "SELECT * FROM AntivirusProduct");
var preVistaResult = searcherPreVista.Get().OfType<ManagementObject>();
var postVistaResult = searcherPostVista.Get().OfType<ManagementObject>();

var instances = preVistaResult.Concat(postVistaResult);

var installedAntivirusses = instances
    .Select(i => i.Properties.OfType<PropertyData>())
    .Where(pd => pd.Any(p => p.Name == "displayName") && pd.Any(p => p.Name == "pathToSignedProductExe"))
    .Select(pd => new
    {
        Name = pd.Single(p => p.Name == "displayName").Value,
        Path = pd.Single(p => p.Name == "pathToSignedProductExe").Value
    })
    .ToArray();

foreach (var antiVirus in installedAntivirusses)
{
    Console.WriteLine("{0}: {1}", antiVirus.Name, antiVirus.Path);
}

要使用此代码,您需要添加以下using语句:

using System;
using System.Linq;
using System.Management;

更多。添加对System.Management的引用。

此代码将生成已安装的所有防病毒软件的列表。列表中的对象将具有名称和路径。如果我运行代码,它会显示以下内容:

  

Microsoft Security Essentials:C:\ Program Files \ Microsoft Security Client \ msseces.exe

答案 1 :(得分:1)

您必须动态创建AV文件夹的路径。

string programFilesDirPath= Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);
string path = Path.Combine(programFilesDirPath,"MyAntivirus","myAntivirus.exe");

希望这有帮助!

相关问题