Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
470 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...