[RESOLVED] Kill spesific proccess useing C#
Hi all,
I have searched up and down and i can only find this VB example on how to do this. needless to say i am having trouble converting it over to c#
http://www.vbforums.com/showpost.php...03&postcount=2
For Each proc As Process In Process.GetProcessesByName("process name here");
proc.CloseMainWindow() //ask the process to exit.;
proc.WaitForExit(10000) //wait up to 10 seconds.
If Not proc.HasExited Then
{
proc.Kill() //force the process to exit.
}
Next proc
I understand the idea but im having trouble with the syntax
do i need to import something to for it to recoginize proccess?
Re: Kill spesific proccess useing C#
Here you go.
C# Code:
foreach(System.Diagnostics.Process proc in System.Diagnostics.Process.GetProcessesByName("process name here"))
{
proc.CloseMainWindow();
proc.WaitForExit(10000);
if (!proc.HasExited)
{
proc.Kill();
}
}
You could import System.Diagnostics if you'd like.
Re: Kill spesific proccess useing C#
hey im having some trouble getting it working
i can see the proccess is WINWORD.EXE in taskman but when i put that in to the textbox1.text it doesnt enter the for loop and proc = null
Re: Kill spesific proccess useing C#
Yeah you have to exclude the '.exe' part, just type WINWORD
Re: Kill spesific proccess useing C#
Ha
I just figured that out on my own like 7 seconds ago and came online to let you know i got it
could have saved my self the day or so of tinkering
Thanks for your help
rate = rate +1
Re: [RESOLVED] Kill spesific proccess useing C#
c# Code:
foreach (System.Diagnostics.Process proc in System.Diagnostics.Process.GetProcessesByName(textBox1.Text))
{
proc.CloseMainWindow();
proc.WaitForExit(10000);
if (!proc.HasExited)
{
proc.Kill();
}
proc.Dispose();
}
Re: [RESOLVED] Kill spesific proccess useing C#
Re: [RESOLVED] Kill spesific proccess useing C#
Im having a small problem
should i post it here or open a new thread?
Ill post it here till told to do otherwise
I am trying to kill the process for a particular session on a Terminal server.
when i run this program it kills ALL the process regardless of session
how can i narrow this down kill the process only on the specific session that it is run on.
For example it killed everything in RED but i only wanted to kill my wordctrl in my session ( in blue)
http://i13.tinypic.com/6bcvou9.png
Re: Kill spesific proccess useing C#
I think im on to something
i found that i can do a proc.session ID that will give me the id of the process
Does anyone know where i can find the current sessionID
i would think it would be under
System.Environment.???? something but i cnat find it.
Re: Kill spesific proccess useing C#
System.Diagnostics.Process.GetCurrentProcess().SessionId;
c# Code:
int ThisSession = System.Diagnostics.Process.GetCurrentProcess().SessionId;
int thisPID = System.Diagnostics.Process.GetCurrentProcess().Id;
foreach (System.Diagnostics.Process proc in System.Diagnostics.Process.GetProcessesByName(application))
{
if (proc.SessionId == ThisSession)
{
proc.CloseMainWindow();
proc.WaitForExit(Wait);
if (!proc.HasExited)
{
proc.Kill();
}
proc.Dispose();
}