views:

13736

answers:

8

I have a scenario where I have to check whether user has already opened Microsoft Word. If he has, then I have to kill the winword.exe process and continue to execute my code.

Does any one have any straight-forward code for killing a process using vb.net or c#?

+13  A: 

You'll want to use the System.Diagnostics.Process.Kill method. You can obtain the process you want using System.Diagnostics.Proccess.GetProcessesByName.

Examples have already been posted here, but I found that the non-.exe version worked better, so something like:

foreach ( Process p in System.Diagnostics.Process.GetProcessesByName("winword") )
{
    try
    {
        p.Kill();
        p.WaitForExit(); // possibly with a timeout
    }
    catch ( Win32Exception winException )
    {
        // process was terminating or can't be terminated - deal with it
    }
    catch ( InvalidOperationException invalidException )
    {
        // process has already exited - might be able to let this one go
     }
}

You probably don't have to deal with NotSupportedException, which suggests that the process is remote.

Blair Conrad
This method won't work without admin or other privileges.
mmr
+6  A: 

Here is an easy example of how to kill all Word Processes.

Process[] procs = Process.GetProcessesByName("winword");

foreach (Process proc in procs)
    proc.Kill();
Nick Berardi
One comment - use "winword" instead of "winword.exe"
Rami
thanks forgot about that, was doing it from memory
Nick Berardi
+1  A: 

Something like this will work:


            foreach ( Process process in Process.GetProcessesByName( "winword.exe" ) )
            {
                    process.Kill();
                    process.WaitForExit();
            }
Tomer Gabel
I don't find the processes when there's a .exe given to GetProcessesByName, although `winword` workd fine.
Blair Conrad
+6  A: 

Killing the Word process outright is possible (see some of the other replies), but outright rude and dangerous: what if the user has important unsaved changes in an open document? Not to mention the stale temporary files this will leave behind...

This is probably as far as you can go in this regard (VB.NET):

    Dim proc = Process.GetProcessesByName("winword")
    For i As Integer = 0 To proc.Count - 1
        proc(i).CloseMainWindow()
    Next i

This will close all open Word windows in an orderly fashion (prompting the user to save his/her work if applicable). Of course, the user can always click 'Cancel' in this scenario, so you should be able to handle this case as well (preferably by putting up a "please close all Word instances, otherwise we can't continue" dialog...)

mdb
A: 

Kill all notepad process

Dim pProcess() As Process = System.Diagnostics.Process.GetProcessesByName("notepad")

For Each p As Process In pProcess
p.Kill()
Next
RBS
+2  A: 

Getting following error while execute the code.

system.security.security exception - Request Failed.

seems like my code does not have permission to terminate the process can anyone help ?

bugBurger
You must be either FullTrust or have the necessary permissions. If you need help with that, ask another question.
Bob King
+1  A: 

You can bypass the security concerns, and create a much politer application by simply checking if the Word process is running, and asking the user to close it, then click a 'Continue' button in your app. This is the approach taken by many installers.

private bool isWordRunning() 
{
    return System.Diagnostics.Process.GetProcessesByName("winword").Length > 0;
}

Of course, you can only do this if your app has a GUI

Chris Lawlor
A: 

What if i want to close a Excel file named "DataSheet.xls" only and leave the other?

Rabin
All the running processes for one application will contain the same data for what the application concerns. You would need the PID in order to achieve that. You could also navigate through it's properties to see if there's one that you can identify.
pedro_cesar