C # Close other program windows or share Process Code
This article mainly introduces how to close other program windows or process code sharing in C #. This article provides two methods and sample code respectively. For more information, see
During winform development, other programs may need to be closed or the process should be closed. I have written an article about this. Some colleagues asked me today, so I will share it with you on the next page.
The following two methods I know should be helpful to you. If you have any other methods, thank you for sharing them.
Method 1
Name of the process to be disabled by ProcName
?
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 terminate the process when the command for sending and closing the window is invalid. Result = true; } } Return result; } |
The above program defines an ArrayList. If 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 lparam ); // SendMessage (hwnd1, WM_CLOSE, 0, 0 ); // Hwnd1 is the handle value returned by the findwindow function. // Wm_close is defined in winuser. h. // 0x0010 is the value of WM_CLOSE SendMessage (hwnd1, 0x0010,0, 0 ); |