Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WqlEventQuery是否包含具有1个参数的构造函数?_C#_.net_Service - Fatal编程技术网

C# WqlEventQuery是否包含具有1个参数的构造函数?

C# WqlEventQuery是否包含具有1个参数的构造函数?,c#,.net,service,C#,.net,Service,我正在尝试使用VS2008在C#中创建一个简单的服务,该服务在计算机进入睡眠模式时创建一个文本文件。我当前的代码抛出以下错误: “SleepNotifierService.WqlEventQuery”不包含接受“1”参数的构造函数 现在我查看了对象浏览器,它看起来确实包含一个参数。这是浏览器必须说的: public WqlEventQuery(字符串queryOrEventClassName) 这是我的密码: using System; using System.Collections.Gene

我正在尝试使用VS2008在C#中创建一个简单的服务,该服务在计算机进入睡眠模式时创建一个文本文件。我当前的代码抛出以下错误:

“SleepNotifierService.WqlEventQuery”不包含接受“1”参数的构造函数

现在我查看了对象浏览器,它看起来确实包含一个参数。这是浏览器必须说的:

public WqlEventQuery(字符串queryOrEventClassName)

这是我的密码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Management;
using System.IO;

namespace SleepNotifierService
{
    public class WqlEventQuery : EventQuery { }

    public partial class Service1 : ServiceBase
    {
        ManagementEventWatcher _watcher;

        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            WqlEventQuery query = new WqlEventQuery("Win32_PowerManagementEvent");
            _watcher = new ManagementEventWatcher(query);
            _watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
            _watcher.Start();
        }

        protected override void OnStop()
        {
            _watcher.Stop();
        }

        void watcher_EventArrived(object sender, EventArrivedEventArgs e)
        {
            try
            {
                int eventType = Convert.ToInt32(e.NewEvent.Properties["EventType"].Value);
                switch (eventType)
                {
                    case 4:
                        Sleep();
                        break;
                    case 7:
                        Resume();
                        break;
                }
            }
            catch (Exception ex)
            {
                //Log(ex.Message);
            }
        }

        public void Sleep()
        {
            StreamWriter SW;
            SW = File.CreateText("c:\\MyTextFile.txt");
            SW.WriteLine("Sleep mode initiated");
            SW.Close();
        }

        public void Resume()
        {
        }

    }
}
我对那个对象浏览器的解释错了吗?一般来说,我对创建服务和C#/.NET还不熟悉,所以这可能是件小事

谢谢你的帮助


Tomek

您使用了错误的
WqlEventQuery
。在中定义了一个,它确实有一个单参数构造函数,但也有自定义的
WqlEventQuery

如果您想使用.NET BCL的类,您必须完全限定它:

var query = new System.Management.WqlEventQuery("Win32_PowerManagementEvent");
甚至在其前面加上关键字:


好电话(8)。为什么在声明该查询变量时使用var关键字?@Tomek为什么不使用?还可以节省一些打字时间
var query = new global::System.Management.WqlEventQuery("Win32_PowerManagementEvent");