C # Implementation closes other program windows or process code sharing

Source: Internet
Author: User
Tags bool

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);
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.