在 C# 中检测打印机配置更改
Detecting Printer configuration changes in c#
有没有办法使用 C# 检测打印机的配置更改?
我在 c# 中有一个线程,它通常处于休眠状态,我希望它在配置发生任何变化时得到通知(比如有人 adds/removes/updates 打印机或有人更改了默认打印机)。一旦收到通知,它会显示简单的消息。
是否可以使用 C#.NET 或使用 WMI?我已经完成了可用的解决方案,但其中 none 似乎适合我的要求。
您可以使用 __InstanceModificationEvent
event and the Win32_Printer
WMI class
监控打印机配置更改
试试这个示例。
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();
}
}
}
有没有办法使用 C# 检测打印机的配置更改?
我在 c# 中有一个线程,它通常处于休眠状态,我希望它在配置发生任何变化时得到通知(比如有人 adds/removes/updates 打印机或有人更改了默认打印机)。一旦收到通知,它会显示简单的消息。
是否可以使用 C#.NET 或使用 WMI?我已经完成了可用的解决方案,但其中 none 似乎适合我的要求。
您可以使用 __InstanceModificationEvent
event and the Win32_Printer
WMI class
试试这个示例。
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();
}
}
}