Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
463 views
in Technique[技术] by (71.8m points)

winforms - Get the list of opened windows C#

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#.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...