Wednesday, January 02, 2008

Kill Instance of Application

Recently I was receiving an issue with an application I am supporting.
 
Problem
The application is a .NET Console application that using Microsoft Word to create reports. The problem is that an exception error would occur if Microsoft Word (WINWORD = Instance Name) was running.
 
Solution

// Code to kill Word issues if WINWORD is running on the current machine

// if word is running the reports will throw an exception so

// we must ensure the Microsoft Word application is not running

Process[] wordProcess = System.Diagnostics.Process.GetProcessesByName("WINWORD");
if
(wordProcess.Length <= 1)
{

 foreach (Process p in wordProcess)

{

p.Kill();

}

}