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

Posted on 2012-08-30
Last Modified: 2014-05-25
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.
Question by:limnath
    LVL 69

    Expert Comment

    by:Éric Moreau

    Author Comment

    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.
    LVL 69

    Expert Comment

    by:Éric Moreau
    download the demo, it has the info of the screens. look at the image, you see 2 monitor.

    Accepted Solution

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    A Bare Metal Image backup allows for the restore of an entire system to a similar or dissimilar hardware. They are highly useful for migrations and disaster recovery. Bare Metal Image backups support Full and Incremental backups. Differential backup…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now