Click here to Skip to main content
16,017,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I need to Close / Kill the already opened Main WinForm application through C# coding, and will support all the operating 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:

C#
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 advance.
Posted
Updated 9-Oct-11 20:33pm
v2
Comments
BillWoodruff 10-Oct-11 6:42am    
What type of scenario are you describing here: what type of processes has your Windows Form application created, or invoked, that are not, now, terminated by Appliction.Exit() ?

1 solution

Use Application.Exit() if you are calling from inside your application.

C#
Process thisProc = null;
string processName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
            
foreach (Process openedProcesses in Process.GetProcesses())
{
	if (openedProcesses.ProcessName.ToLower().Contains(processName.ToLower()))
	{
		thisProc = openedProcesses;
		//thisProc.CloseMainWindow();
		thisProc.Kill();
	}
}
 
Share this answer
 
v2
Comments
Member 3827009 10-Oct-11 5:56am    
It is not with the same instance.
We want with different instance.
Mehdi Gholam 10-Oct-11 6:13am    
Try the edited version.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900