How to close/Kill the already opened Main winform in C#

Oct 10 2011 2:32 AM
Hi,
I need to Close / Kill the already opened Main Winform application through C# coding, and will sopport all the operatingg system (Win 7 32 & 64 bit, Vista 32 & 64 Bit etc).
I already tried the below code but some times it didn't work in XP.
And rest OS this is not working. Please find below code:
Process thisProc = null;
string processName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
 
foreach (Process openedProcesses in Process.GetProcesses())
{
 if (openedProcesses.ProcessName.Contains(processName))
 {
 thisProc = openedProcesses;
 thisProc.CloseMainWindow();
 thisProc.Kill();
 }
}
Thanks in Adavance.

Answers (2)