It is a small part of my program where my program needs to start the program from DataGridView (Content Clicked Event) and its just executing that program perfectly but is not able to close it because some of incoming programs don't have same process name as file name. I tried getting processid too but it throws following error (can you please provide me a working code because i can get the id of my winform process but how can i get the processid of externally launched app from my program. i tried it it throws the following error
An unhandled exception of type 'System.ArgumentException' occurred in System.dll Additional information: Process with an Id of 16924 is not running.)"
the code in which i am getting process id but fails is below
private void button1_Click(object sender, EventArgs e)
{
var processid = Process.Start("Calc");
pn =processid.ProcessName;
pid = processid.Id;
}
int pid;
String pn;
private void button2_Click(object sender, EventArgs e)
{
var process1 = Process.GetProcessById(pid);
process1.Kill();
}
The dummy code is below.
I have already tried:
private void button1_Click(object sender, EventArgs e)
{
Process.Start("Calc");
}
private void button2_Click(object sender, EventArgs e)
{
foreach (var process in Process.GetProcessesByName("Calc"))
{
process.Kill();
}
}
My Code:
private void button1_Click(object sender, EventArgs e)
{
Process.Start("THIS PATH WILL COME FROM DATABASE");
}
private void button2_Click(object sender, EventArgs e)
{
foreach (var process in Process.GetProcessesByName("PROCESS NAME WHICH MY PROGRAM STARTED"))
{
process.Kill();
}
}