Is a process running on a remote machine?

后端 未结 3 1127
野趣味
野趣味 2021-02-09 10:13

I have three remote PC\'s to which I remotely connect. I am trying to write a simple Windows application that would display in a single window whether a particular process is ru

相关标签:
3条回答
  • 2021-02-09 10:23

    You can filter the name of the process to watch in the WQL sentence, so you can write something like this

     SelectQuery query = new SelectQuery("select * from Win32_Process Where Name='Chrome.exe'");
    

    Try this sample app

    using System;
    using System.Collections.Generic;
    using System.Management;
    using System.Text;
    
    namespace GetWMI_Info
    {
        class Program
        {
    
            static void Main(string[] args)
            {
                try
                {
                    string ComputerName = "localhost";
                    ManagementScope Scope;                
    
                    if (!ComputerName.Equals("localhost", StringComparison.OrdinalIgnoreCase)) 
                    {
                        ConnectionOptions Conn = new ConnectionOptions();
                        Conn.Username  = "";
                        Conn.Password  = "";
                        Conn.Authority = "ntlmdomain:DOMAIN";
                        Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), Conn);
                    }
                    else
                        Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), null);
    
                    Scope.Connect();
                    ObjectQuery Query = new ObjectQuery("SELECT * FROM Win32_Process Where Name='Chrome.exe'");
                    ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);
    
    
                    foreach (ManagementObject WmiObject in Searcher.Get())
                    {
                      //for each instance found, do something  
                      Console.WriteLine("{0,-35} {1,-40}","Name",WmiObject["Name"]);
    
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(String.Format("Exception {0} Trace {1}",e.Message,e.StackTrace));
                }
                Console.WriteLine("Press Enter to exit");
                Console.Read();
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-09 10:28

    Try Process.GetProcesses("chrome", "computerName");

    Defined in System.Diagnostics.Process as

    public static Process[] GetProcessesByName(
       string processName,
       string machineName)
    
    0 讨论(0)
  • 2021-02-09 10:40

    In your foreach, try this:

    Console.WriteLine(process["Name"]);
    
    0 讨论(0)
提交回复
热议问题