Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the list of opened windows C#

Tags:

c#

winforms

During the installation of any application. Generally the user was asked to close all windows before start of installation. if not the installation will stop at the middle and ask the user to close all opened windows. I was asked to add a code in a XXX application. When application is running and if user opened any other window (ex: Explore, browser, word etc..) then the application should throw a window saying that you have opened the list of windows. I requerst you please suggest me how to start in C#.

like image 693
user517206 Avatar asked Dec 15 '10 14:12

user517206


3 Answers

Test this:

var openWindowProcesses = System.Diagnostics.Process.GetProcesses()
   .Where(p => p.MainWindowHandle != IntPtr.Zero && p.ProcessName != "explorer");

The openWindowProcesses should contains all open application which they have an active main window.

I put p.ProcessName != "explorer" in the where expression because the explorer is the main process of the Desktop and it should never be closed.

To watching execution of the processes you can use ManagementEventWatcher class. See this please.

like image 98
Amir Karimi Avatar answered Nov 11 '22 08:11

Amir Karimi


You can use System.Diagnostics.Process class to get the information of all processes which are running in your machine. Then you can try to find if intended application/process is running or not.

You can either use GetProcesses() or GetProcessByName() method. Refer this msdn link for reference. I am sure that there can be more efficient way of achieving the same. HTH

like image 28
Pradeep Avatar answered Nov 11 '22 08:11

Pradeep


Set up a foreach loop like this to enumerate over all the open applications on your system (that have a visible main window)

foreach (var process in Process.GetProcesses().Where( p => p.MainWindowHandle != IntPtr.Zero)) {
    //do something with the process here. To display it's name, use process.MainWindowTitle
  }
like image 1
Øyvind Bråthen Avatar answered Nov 11 '22 06:11

Øyvind Bråthen