2

我正在尝试使用 C# 获取 java Process 对象。问题是我的计算机上运行了几个 java 进程。

以下是我选择获取流程的方式:

Process[] processes = Process.GetProcessesByName("java");
foreach(Process proc in processes){
    //I need a filter here to get the correct process.
}

Java 进程也由我的 C# 程序控制,如下所示:

 ProcessStartInfo startInfo = new ProcessStartInfo();
 startInfo.FileName = javahome + "\\bin\\java.exe";
 startInfo.Arguments = "-jar Example.jar port=88888";
 startInfo.WorkingDirectory = "\\testFolder";
 startInfo.UseShellExecute = false;
 startInfo.CreateNoWindow = true;
 Process proc = new Process();
 proc.StartInfo = startInfo;
 proc.Start();

我想要的是通过 Process 数组来检查哪个与我在另一个程序中启动的 Process 对象具有相同的参数。但问题是当我这样做时:

 Console.WriteLine(proc.StartInfo.Arguments);

我发现里面什么都没有,即使我知道这是我在另一个程序中启动的过程。这让我很困惑。

有谁知道这个问题?

4

3 回答 3

2

你不能那样做。当您启动一个进程时,将该进程的处理程序保存在字典中,其中值是进程参数,这是我看到的唯一存档方法。

        Dictionary<IntPtr, string> processArguments = new Dictionary<IntPtr,string>();

        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = javahome + "\\bin\\java.exe";
        startInfo.Arguments = "-jar Example.jar port=88888";
        startInfo.WorkingDirectory = "\\testFolder";
        startInfo.UseShellExecute = false;
        startInfo.CreateNoWindow = true;
        Process proc = new Process();
        proc.StartInfo = startInfo;
        proc.Start();

        processArguments.Add(proc.Handle, javahome + "\\bin\\java.exe");

....

        Process[] processes = Process.GetProcessesByName("java");
        foreach (Process proc in processes)
        {
            var arguments = processArguments.Where(x => x.Key.Equals(proc.Handle)).FirstOrDefault().Value;
        }
于 2013-05-09T11:26:55.633 回答
0

您可以通过使用 LINQ 来尝试这样的操作,如下所示:

Process[] processes = Process.GetProcessesByName("java");
var fileteredProcess = from pro in processes
                        where (pro.StartInfo.WorkingDirectory == "workingDIR") &&
                        (pro.StartInfo.Arguments == "Arguments")
                        select pro;

foreach (var proc in fileteredProcess)
{

}
于 2013-05-09T11:37:48.907 回答
0

这是使用 WMI 的替代解决方案:

Process[] processes;
Process selectedProc = null;
int selectedProcId = 0;

//  http://wutils.com/wmi/
//  using System.Management;
ManagementScope scope = new ManagementScope("\\\\.\\ROOT\\cimv2");
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Process "
                                  + "WHERE Name = 'java.exe'");
ManagementObjectSearcher searcher = 
        new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();

foreach (ManagementObject m in queryCollection)
{
        // access properties of the WMI object
        Console.WriteLine("CommandLine : {0}", m["CommandLine"]);
        Console.WriteLine("ProcessId : {0}", m["ProcessId"]);

        if (m["CommandLine"].ToString().Contains("my pattern"))
        {
            selectedProcId = int.Parse(m["ProcessId"].ToString());
            selectedProc = Process.GetProcessById(selectedProcId);
            break;
        }
}

if (selectedProc != null)
{
        Console.WriteLine("Proc title {0}", selectedProc.MainWindowTitle);
}
于 2013-05-09T12:19:53.043 回答