C # Operations window through process name/process ID

Source: Internet
Author: User

1. Determine if the window exists
1     Private BOOLiswindowexist (IntPtr handle)2     {3         return(! (GetWindow (NewHandleRef ( This, handle),4)! = IntPtr.Zero) && iswindowvisible (NewHandleRef ( This, handle)));4     }5 6[DllImport ("user32.dll", CharSet = CharSet.Auto, exactspelling =true)]7      Public Static externIntPtr GetWindow (HandleRef hWnd,intucmd);8 9[DllImport ("user32.dll", CharSet =CharSet.Auto)]Ten      Public Static extern BOOLIsWindowVisible (HandleRef hWnd);
2. Get the Window handle
1     /// <summary>2     ///get application window handle3     /// </summary>4     /// <param name= "ProcessId" ></param>5     /// <returns></returns>6     PrivateIntPtr Getwindowhandle (intprocessId)7     {8         varWindowHandle =IntPtr.Zero;9Enumthreadwindowscallback Windowscallback =NewEnumthreadwindowscallback (FindMainWindow);Ten EnumWindows (Windowscallback, IntPtr.Zero); One         //Keep the Loop A GC. KeepAlive (windowscallback); -  -         BOOLFindMainWindow (IntPtr handle, IntPtr extraparameter) the         { -             intnum; -GetWindowThreadProcessId (NewHandleRef ( This, handle), outnum); -             if(num = = ProcessId &&iswindowexist (handle)) +             { -WindowHandle =handle; +                 return true; A             } at             return false; -         } -  -         returnWindowHandle; -     } -      Public Delegate BOOLEnumthreadwindowscallback (IntPtr hWnd, IntPtr lParam); in  -[DllImport ("user32.dll", CharSet = CharSet.Auto, SetLastError =true)] to      Public Static extern BOOLEnumWindows (Enumthreadwindowscallback callback, IntPtr extradata); +  -[DllImport ("user32.dll", CharSet = CharSet.Auto, SetLastError =true)] the      Public Static extern intGetWindowThreadProcessId (HandleRef handle, out intPROCESSID);
3. Close the application window

Close the app window based on the process ID:

1     /// <summary>2     ///Close Program main window3     /// </summary>4     /// <param name= "ProcessId" >Process ID</param>5      Public voidCloseWindow (intprocessId)6     {7         varMWH =Getwindowhandle (processId);8SendMessage (MWh, Mw_close,0,0);9     }Ten     Const intMw_close =0x0010; One  A[DllImport ("User32.dll", EntryPoint ="SendMessage")] -     Private Static extern intSendMessage (IntPtr hWnd,intMSG,intWParam,intLParam);

Close all windows for this process name:

1          Public voidCloseallwindows (stringprocessName)2         {3process[] Processlist =Process.getprocessesbyname (processName);4             foreach(Process processinchprocesslist)5             {6 CloseMainWindow (process. ID);7             }8}

Of course, the direct kill process is the quickest way:

1     /// <summary>2     ///Close All Processes3     /// </summary>4     /// <param name= "ProcessName" ></param>5     /// <returns></returns>6      Public Static BOOLCloseallprocesses (stringprocessName)7     {8         Try9         {Tenprocess[] Pseainotes =Process.getprocessesbyname (processName); One             foreach(Process Pseainoteinchpseainotes) A             { - Pseainote.kill (); -             } the         } -         Catch -         { -             return false; +         } -  +         return true; A}
4. Restart the program
1     stringAppfilename =CurrentClientProcess.MainModule.FileName;2 3Process newclient =NewProcess ();4NewClient.StartInfo.FileName =Appfilename;5     //set the initial directory of the process to start6NewClient.StartInfo.WorkingDirectory =System.Windows.Forms.Application.ExecutablePath;7     8     //Start the program9 Currentclientprocess.kill ();TenNewclient.start ();

Processing of sending/receiving messages between windows, refer to C # cross-process communication

C # Operations window through process name/process ID

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.