The problem is that on the text file i see all the results - True
or Not
.
Even when i turned off the monitor for 5-10 seconds and turned it on on, then the text file shows True
.
Not sure why it's not detecting it.
I tried this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Net;
using System.Diagnostics;
using System.Management;
namespace Detect_Monitor_Settings
{
public partial class Form1 : Form
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);
[FlagsAttribute]
public enum EXECUTION_STATE : uint
{
ES_AWAYMODE_REQUIRED = 0x00000040,
ES_CONTINUOUS = 0x80000000,
ES_DISPLAY_REQUIRED = 0x00000002,
ES_SYSTEM_REQUIRED = 0x00000001
// Legacy flag, should not be used.
// ES_USER_PRESENT = 0x00000004
}
StreamWriter w = new StreamWriter(@"e:\monitordetector.txt");
public Form1()
{
InitializeComponent();
}
private void DetectScreenName()
{
if (stopdetecting == false)
{
var query = "select * from WmiMonitorBasicDisplayParams";
using (var wmiSearcher = new ManagementObjectSearcher("\\root\\wmi", query))
{
var results = wmiSearcher.Get();
foreach (ManagementObject wmiObj in results)
{
// get the "Active" property and cast to a boolean, which should
// tell us if the display is active. I've interpreted this to mean "on"
var active = (Boolean)wmiObj["Active"];
w.WriteLine(active.ToString());
}
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
bool stopdetecting = false;
int counttimer1 = 0;
private void timer1_Tick(object sender, EventArgs e)
{
DetectScreenName();
counttimer1 += 1;
}
private void button1_Click(object sender, EventArgs e)
{
w.Close();
stopdetecting = true;
timer1.Stop();
}
}
}