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#.
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.
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
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With