This article mainly introduces the C # implementation to close other program windows or process code sharing, this article gives two methods, and to show the example code, need friends can refer to the
In the process of WinForm development, sometimes you will need to close other programs or shut down the process, previously wrote a related article, today, a colleague asked, so in the time to turn out and share with you.
Here are two ways I know, should be helpful to everyone, if a friend know other ways, thank you share.
Method 1
ProcName process name that needs to be closed
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
private bool Closeproc (string procname) {bool result = false; System.Collections.ArrayList proclist = new System.Collections.ArrayList (); String tempname = ""; foreach (System.Diagnostics.Process thisproc in System.Diagnostics.Process.GetProcesses ()) {tempname = Thisproc.processname; Proclist.add (Tempname); if (tempname = = procname) {if (!thisproc.closemainwindow ()) Thisproc.kill ();//forcibly end process result = True when the Send close window command is invalid; } return result; } |
The above program defines a ArrayList, when you do not know the specific name of the process to be closed, you can put the value of ArrayList in a ListBox or other controls to select the process to end.
Method 2
Declare at the top of the class body:
?
1 2 3 4 5 6 7 8 |
[DllImport ("user32.dll", CharSet=CharSet.Auto)] public static extern int SendMessage (int hWnd, int msg, int wParam, int LP Aram); SendMessage (hwnd1,wm_close,0,0); Hwnd1 is the value of the handle that you return with the FindWindow function//wm_close defined in Winuser.h//0x0010 is Wm_close (SendMessage); |