|
-
May 31st, 2007, 02:56 PM
#1
Thread Starter
Fanatic Member
[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?
Last edited by Crash893; Jun 4th, 2007 at 12:44 PM.
-
May 31st, 2007, 05:18 PM
#2
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.
-
Jun 1st, 2007, 11:17 AM
#3
Thread Starter
Fanatic Member
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
-
Jun 1st, 2007, 03:48 PM
#4
Re: Kill spesific proccess useing C#
Yeah you have to exclude the '.exe' part, just type WINWORD
-
Jun 3rd, 2007, 06:16 PM
#5
Thread Starter
Fanatic Member
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
-
Jun 3rd, 2007, 06:19 PM
#6
Thread Starter
Fanatic Member
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();
}
-
Jun 3rd, 2007, 06:29 PM
#7
Re: [RESOLVED] Kill spesific proccess useing C#
Good news
-
Jun 3rd, 2007, 07:43 PM
#8
Thread Starter
Fanatic Member
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)
Last edited by Crash893; Jun 3rd, 2007 at 08:14 PM.
-
Jun 4th, 2007, 12:46 PM
#9
Thread Starter
Fanatic Member
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.
-
Jun 4th, 2007, 07:28 PM
#10
Thread Starter
Fanatic Member
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();
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|