Nation
Nation

Reputation: 21

EventHandler to get HDMI connectivity in windows application

I am looking for event handler to get the information when HDMI is connected or disconnected using c#/WPF like how myNetworkAvailabilityChangeHandler is used to detect when internet gots disconnected or connected.

Upvotes: 0

Views: 572

Answers (1)

Satyam Mishra
Satyam Mishra

Reputation: 361

There is no direct answer to this but you can use the SystemEvents to achieve this.

Code in C# :

main()
{
SystemEvents.DisplaySettingsChanged += new 
EventHandler(SystemEvents_DisplaySettingsChanged); 
}       
private void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
    {
        int HDMI_Monitors = 0;
        ManagementClass mClass = new ManagementClass(@"\\localhost\ROOT\WMI:WmiMonitorConnectionParams");
        foreach (ManagementObject mObject in mClass.GetInstances())
        {
            var ss = mObject["VideoOutputTechnology"];
            if(ss.ToString().StartsWith("5"))
            {
                int HDMIport = Convert.ToInt32(ss);
                if (HDMIport == 5)
                {
                    HDMI_Monitors += 1;
                }
            }
         }
    }

You can use the model class to keep on updating the HDMI status. So every time your HDMI will connect or disconnect SystemEvents_DisplaySettingsChanged will trigger and then it will check the HDMI connection.

Upvotes: 1

Related Questions