【问题标题】:Detecting Printer configuration changes in c#在 C# 中检测打印机配置更改
【发布时间】:2015-07-28 12:28:07
【问题描述】:

有没有办法使用 C# 检测打印机中的配置更改?

我在 c# 中有一个线程,它通常处于休眠状态,我希望在配置发生任何更改时通知它(例如有人添加/删除/更新打印机或有人更改了默认打印机)。一旦收到通知,它将显示简单的消息。

这是否可能使用 C#.NET 或使用 WMI?我已经浏览了可用的解决方案,但它们似乎都不适合我的要求。

【问题讨论】:

  • 如果您写下您已经尝试过的解决方案是什么以及为什么它们不适合您的要求,您可能会得到更好的答案
  • 就像我说的,我正在寻找一种方法来做到这一点。我已经查看了各种论坛中提供的帮助,因为它们都没有帮助解决我的目的,所以我没有实施任何帮助。但是对于信息,我能到达的最接近的是Here
  • 从您链接的问题看来,带有 PRINTER_CHANGE_PRINTER 的 FindFirstPrinterChangeNotification 将检测添加/删除打印机,该问题的答案告诉您如何检测默认打印机更改
  • 是的,但这是非常具体的事情。我还在其他论坛和博客上搜索。

标签: c# wmi printers


【解决方案1】:

您可以使用__InstanceModificationEvent 事件和Win32_Printer WMI 类监控打印机配置更改

试试这个示例。

using System;
using System.Collections.Generic;
using System.Management;
using System.Text;


namespace GetWMI_Info
{
    public class EventWatcherAsync
    {
        private void WmiEventHandler(object sender, EventArrivedEventArgs e)
        {
            Console.WriteLine("TargetInstance.Name :       " + ((ManagementBaseObject)e.NewEvent.Properties["TargetInstance"].Value)["Name"]);

        }

        public EventWatcherAsync()
        {
            try
            {
                string ComputerName = "localhost";
                string WmiQuery;
                ManagementEventWatcher Watcher;
                ManagementScope Scope;


                if (!ComputerName.Equals("localhost", StringComparison.OrdinalIgnoreCase))
                {
                    ConnectionOptions Conn = new ConnectionOptions();
                    Conn.Username = "";
                    Conn.Password = "";
                    Conn.Authority = "ntlmdomain:DOMAIN";
                    Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), Conn);
                }
                else
                    Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), null);
                Scope.Connect();

                WmiQuery = "Select * From __InstanceModificationEvent Within 1 " +
                "Where TargetInstance ISA 'Win32_Printer' ";

                Watcher = new ManagementEventWatcher(Scope, new EventQuery(WmiQuery));
                Watcher.EventArrived += new EventArrivedEventHandler(this.WmiEventHandler);
                Watcher.Start();
                Console.Read();
                Watcher.Stop();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception {0} Trace {1}", e.Message, e.StackTrace);
            }

        }

        public static void Main(string[] args)
        {
            Console.WriteLine("Listening {0}", "__InstanceModificationEvent");
            Console.WriteLine("Press Enter to exit");
            EventWatcherAsync eventWatcher = new EventWatcherAsync();
            Console.Read();
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-14
    • 2022-11-04
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 2019-05-10
    相关资源
    最近更新 更多