简体   繁体   中英

How to get PID of started App in C#

I have got a few pairs of the identical apps.

It looks like

Pair 1: App A + App B

Pair 2: App A + App B

and etc

Each App A starts App B from its folder. (They are both at the same folder.)

So I start it like this

Code of App A

Process p = new Process();
p.StartInfo.FileName = FileManager.AppDirectoryName + "\\" + AppB;
p.Start();

And I also stop App B like this.

foreach (Process p in Process.GetProcesses())
{  
    if (p.ProcessName == AppB)
    {
        p.Kill();
        return;
    }
} 

The problem is that at the same time could be many App B executing so this method does not allow to detect target App B to kill.

I assume to use PID of App B to kill it. But I don't know how to obtain PID at the moment to start App B...

Any clue?

启动应用程序后,Process对象上的Id属性将指示新启动的进程的PID

var PID = p.Id;

Save a reference to the Process object:

Process p = new Process();
p.StartInfo.FileName = @"c:\windows\notepad.exe";
p.Start();

// ...

p.Kill();

or remember its PID:

Process p = new Process();
p.StartInfo.FileName = @"c:\windows\notepad.exe";
p.Start();

long pid = p.Id;

// ...

Process.GetProcessById(pid).Kill();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM