WMI C# Question: How to get Display properties of Multiple Monitors Attached to my Computer

Hello everyone,

I have a project that requires me to gather inventory data for my computer. I am having problems trying to get the properties of the monitors attached to my computer.

I have one computer that can potentially use a max of 4 monitors, wherein I have a primary monitor and 3 other potential monitors which act as extension screens when I move my mouse to them ( depending on whether they are connected or not ).

I need to grab the following information from each monitor :

* DeviceId,
* Name,
* Horizontal and Vertical resolution,
* Whether or not the monitor is the primary one ( a true or false value will suffice ).

I tried using the WMI, Win32_DesktopMonitor class but it is only giving me ONE monitor's results ( the primary monitor ). It does not seem to grab data for other attached monitors other than the primary one.

I then tried using the Win32_PnPEntity class.

This works, and gives me the number of monitors attached to my computer, but it only gives me Name and DeviceId ( no horizontal and vertical resolution information in this class ).

I then tried the Screen class (using the System.Windows.Forms.Screen), however the result is erratic. If I ran the program as a pure console application, it works and gives me the right number of monitors attached to my computer, when I ran the same program as a windows service, it does not. It only gives me ONE ( the primary monitor ).

Does anyone know of a Class provided by .NET 4 that will help me gather the information I need?

 I am using the following environment:

1) .NET 4
2) Visual Studio 2010
3) C# programming language
4) Windows Service Application

Any suggestions and advise will be highly appreciated. If you need to look at the code I used to run it, I will be glad to post it too. If requested.

Thank you very much everyone for reading this and for any help or guidance.
Who is Participating?
limnathAuthor Commented:
I believe I am beginning to understand the problem...

There is a difference between a logged on user and a Windows service. When you run a Windows service, it runs in a different desktop than a logged in User. In fact, each logged in user runs in its own desktop.

As such, the reason that you only get one monitor returned when running as a windows service is because that desktop (i.e. the windows service desktop(0) ) has a default non-visible monitor assigned to it.

 The first logged on user gets a different desktop(1) and might have two monitors configured and the next user (running in desktop(2)) might not have the "extend desktop to other monitors" setting checked so it only sees one monitor.

So monitor count would be something like:
Service 1
User1 2
User2 1

If you run your monitor tool under the service, it will run under the context of the service account and therefore it returns 1 monitor. Your service code could run the tool (Process.Start or runas) under a different user account, but then the service would need the login account credentials of the user you wish to run under.

So I believe there is no easy way to solve this problem and unless someone comes up with a better solution, I would have to conclude ( at least at this point in time ) that we CANNOT use the Windows Service approach to solve this problem.
limnathAuthor Commented:
Thanks for the link you provided.

I looked at it. However the link mentions nothing about getting Display monitor information. It talks about getting  CPU, the RAM, the hard disk, Windows version, which I already know how to do without problems.

I need a solution to my problem at hand, which is to get Display properties of Multiple Monitors Attached to my Computer.
Éric MoreauSenior .Net ConsultantCommented:
download the demo, it has the info of the screens. look at the image, you see 2 monitor.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.