During winform development, sometimes you need to close other programs or close processes. I wrote a related introduction article before. Today, a colleague asked about it, so I took it out and shared it with you.
The following are two methods I know, which should be helpful to everyone. If any friends know about other methods, thank you for sharing them.
Method 1
ProcName The name of the process that needs to be closed
private bool closeProc(string ProcName) { bool result = false; procList = new (); string tempName = ""; foreach ( thisProc in ()) { tempName = ; (tempName); if (tempName == ProcName) { if (!()) (); // Forced end process when sending the closing window command is invalid result = true; } } return result; }
The above program defines an ArrayList. When you do not know the specific name of the process you want to close, you can place the value in the ArrayList in a listbox or other control to select the process to end.
Method 2
At the top of the class body:
[DllImport("", CharSet=)] public static extern int SendMessage(int hWnd, int msg, int wParam, int lparam); //SendMessage(hwnd1,WM_CLOSE,0,0); //hwnd1 is the handle value you return with the findwindow function //wm_close is defined inside //0x0010 is the value of WM_CLOSE SendMessage(hwnd1,0x0010,0,0);