Проблема в том, что в текстовом файле я вижу все результаты - True
или Not
. Даже когда я выключил монитор в течение 5-10 секунд и включил его, текстовый файл показывает True
.Как определить, выключен или выключен монитор?
Не знаете, почему это не обнаружено.
Я попытался это:
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();
}
}
}
Я думаю, что ваша проблема здесь: '// сообщите нам, активен ли дисплей. Я интерпретировал это как «на». Откуда у вас это предположение? Можете ли вы предоставить некоторую документацию? –
Это уже хорошо освещено в SO Q + A, нечего повторять. Google "wmi обнаружить монитор включен", чтобы найти их, пять лучших хитов все вопросы. Достаточно знать, чтобы начать искать другой проект. –