获取已安装程序的进程名称

问题描述 投票:0回答:3

如何获取Windows(10)中已安装程序对应的进程名?现在,我正在使用这个:

string uninstallKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"; 
using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(uninstallKey))
{
    foreach (string skName in rk.GetSubKeyNames())
    {
        using (RegistryKey sk = rk.OpenSubKey(skName))
        {
            //returns installed programs
        }
    }
}

返回已安装的软件。尽管并未显示每个已安装的程序,但我如何获取进程的名称,就像任务管理器中显示的那样,如果启动该程序就会启动?

我想将应用程序列入黑名单。如果应用程序启动,它会将其进程与黑名单进行比较。如果进程与列表中的条目匹配,则该进程将被终止。

c# wpf windows
3个回答
0
投票

使用

GetProcesses
类的静态方法
Process
为本地计算机上每个正在运行的进程创建组件。

你可以这样得到他们的名字:

var processNames = Process.GetProcesses().Select(x => x.ProcessName).ToList();

更多关于

Process
课程的信息请点击这里: https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process?view=net-6.0


0
投票

您应该考虑使用Windows集成功能通过注册表阻止应用程序。您可以通过编程方式创建此类条目。

但是,您可以实现自己的方法,但您必须知道您无法阻止应用程序开始使用您的方法。你只能在它启动并分配资源后杀死它。

  1. 首先创建您的黑名单:收集所有已安装的应用程序路径并让用户选择要加入黑名单的应用程序(参见
    CreateInstalledApplicationIndex
    方法)。
  2. 使用WMI通过注册相应的事件处理程序来观察任何进程的启动。
  3. 在事件处理程序中检索已启动的
    Process
    并将其文件名与列入黑名单的文件名进行比较,以识别和处理禁止的进程。
private List<FileInfo> InstallationInfos { get; } = new List<FileInfo>();
private List<FileInfo> BlacklistedExecutables { get; } = new List<FileInfo>();

public void ApplyBlacklist()
{
  CreateInstalledApplicationIndex();
  WatchProcessStarts();
}

private void CreateInstalledApplicationIndex()
{
  string uninstallKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
  using RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(uninstallKey);

  foreach (string subKeyName in registryKey.GetSubKeyNames())
  {
    using RegistryKey subKey = registryKey.OpenSubKey(subKeyName);

    var installationPath = subKey.GetValue("InstallLocation") as string;
    if (string.IsNullOrWhiteSpace(installationPath))
    {
      continue;
    }

    IEnumerable<FileInfo> fileInfos = Enumerable.Empty<FileInfo>();
    try
    {
      var installationDirectoryInfo = new DirectoryInfo(installationPath);
      fileInfos = installationDirectoryInfo.EnumerateFiles("*.exe", new EnumerationOptions());
    }
    catch (IOException)
    {
      continue;
    }

    foreach (FileInfo fileInfo in fileInfos)
    {
      this.InstallationInfos.Add(fileInfo);

      // For demo, all executables are blacklisted.
      // TODO::Let user fill Blacklisted collection.
      this.BlacklistedExecutables.Add(fileInfo);
    }
  }
}

private void WatchProcessStarts()
{
  WqlEventQuery query = new WqlEventQuery("SELECT * FROM Win32_ProcessStartTrace");
  ManagementEventWatcher watcher = new ManagementEventWatcher(query);
  watcher.EventArrived += OnProcessStarted;

  // Start listening for process start events
  watcher.Start();
                
  // Stop listening for process start events
  //watcher.Stop();
}

private void OnProcessStarted(object sender, EventArrivedEventArgs e)
{
  uint startedProcessId = (uint)e.NewEvent["ProcessID"];

  // Note: Convert.ToInt32 will throw an OverflowException
  // in case uint does not fit into an int.
  // You must decide whether to handle this particular exception or to let it crash your application.
  // Since it is very very unlikely that a machine runs Int32.MaxValue processes, 
  // I recommend not to handle this exception.
  Process startedProcess = Process.GetProcessById(Convert.ToInt32(startedProcessId));

  bool isProcessBlacklisted = this.BlacklistedExecutables
    .Select(fileInfo => fileInfo.FullName)
    .Contains(startedProcess.MainModule.FileName);

  // TODO::Handle blacklisted process e.g., by killing it
  if (isProcessBlacklisted)
  {
    startedProcess.Kill(entireProcessTree: true);
  }
}

您可能必须以管理员身份运行应用程序才能观察进程启动并终止它们。在这种情况下,请确保提示用户通过使用管理员权限重新启动应用程序来提升应用程序的权限。


0
投票

我得到了一个如下所示的解决方案:

首先我得到所有已安装的程序基于此

public static void LoadInstalledPrograms() 
{
    var FOLDERID_AppsFolder = new Guid("{1e87508d-89c2-42f0-8a7e-645a0f50ca58}");
    ShellObject appsFolder = (ShellObject)KnownFolderHelper.FromKnownFolderId(FOLDERID_AppsFolder);
    foreach (var app in (IKnownFolder)appsFolder)
    {
        //regular installed programs
        if (app.Properties.System.Link.TargetParsingPath.Value != null)
        {
            AddToInstalledProgramsList(app.Name, app.Properties.System.Link.TargetParsingPath.Value, "reg");
        }
        //Windows apps/Microsoft store apps
        /*else
        {
        AddToInstalledProgramsList(app.Name, app.Properties.GetProperty("System.AppUserModel.PackageInstallPath").ValueAsObject.ToString(), "win");
        }*/
   }
}

然后将它们写入字典,由后台工作人员观察该字典,并杀死列表中的每个进程

static Dictionary<String, String> programs = new Dictionary<String, String>();
public static void AddToInstalledProgramsList(string programName, string programPath, string programType) 
{
    string processName = "";
    if (programType == "reg")
    {
        programPath = programPath.Replace("/", "\\");
        processName = programPath.Split("\\").Last();

        if (!programs.ContainsKey(programName))
        {
            programs.Add(programName, processName);
        }
        else
        {
            AddDuplicateEntry(programName, processName, 1);
        }
    }
    else if (programType == "win")
    {
        //...
    }
    Debug.WriteLine(programName + ": " + processName);
}

如果我偶然发现这种方法的问题,我将更新此线程。

© www.soinside.com 2019 - 2024. All rights reserved.