C# 如何找到&;是否在同一WiFi网络上安装打印机?
在我的应用程序(win10,WPF)上,我允许用户打印报告。C# 如何找到&;是否在同一WiFi网络上安装打印机?,c#,wpf,printers,network-printers,printdialog,C#,Wpf,Printers,Network Printers,Printdialog,在我的应用程序(win10,WPF)上,我允许用户打印报告。 但是 当本地计算机上有尚未配置的打印机时-这是不可能的。 (当然,打印机是在网络上激活的,通过使用settings->addprinter&sccaners可以找到并安装打印机) 我在网上搜索了几天,寻找一种方法: 1. 使用ManagementObjectSearcher(“从Win32\U打印机中选择*) 但仅在本地安装了打印选项 二, 尝试了以下代码-但我不知道服务器或打印机名称 (我可能知道用户运行我的应用程序的网络名称-但它
但是
当本地计算机上有尚未配置的打印机时-这是不可能的。
(当然,打印机是在网络上激活的,通过使用
settings->addprinter&sccaners
可以找到并安装打印机)
我在网上搜索了几天,寻找一种方法:1.
使用ManagementObjectSearcher(“从Win32\U打印机中选择*)
但仅在本地安装了打印选项 二, 尝试了以下代码-但我不知道服务器或打印机名称
(我可能知道用户运行我的应用程序的网络名称-但它可以是任何类型的打印机…)
使用(ManagementClass Win32打印机=新的ManagementClass(“Win32\U打印机”))
{
使用(ManagementBaseObject inputParam=win32Printer.GetMethodParameters(“AddPrinterConnection”))
{
//将和替换为实际的服务器和
//打印机名称。
inputParam.SetPropertyValue(“名称”、“名称”);
使用(ManagementBaseObject结果)=
(ManagementBaseObject)win32Printer.InvokeMethod(“AddPrinterConnection”,inputParam,null))
{
uint errorCode=(uint)result.Properties[“returnValue”].Value;
开关(错误代码)
{
案例0:
Console.Out.WriteLine(“已成功连接打印机”);
打破
案例5:
Console.Out.WriteLine(“访问被拒绝”);
打破
案例123:
Console.Out.WriteLine(“文件名、目录名或卷标语法不正确。”);
打破
判例1801:
Console.Out.WriteLine(“无效的打印机名称”);
打破
案例1930:
Console.Out.WriteLine(“不兼容的打印机驱动程序”);
打破
案例3019:
Console.Out.WriteLine(“在系统上找不到指定的打印机驱动程序,需要下载。”);
打破
}
}
}
}
添加打印机和扫描仪
),我获得了打印机的mac地址,即使在取消安装后,我也可以获得打印机的“IP地址(如建议)PrintDialog
打印机和扫描仪
屏幕打开窗口设置
窗口发现:
Process.Start(“ms设置:打印机”)代码>
另一件事——正如@sajithsager在上面的评论()中所建议的那样: 我删除了我的2台网络打印机中的1台,1台HP和1台Cannon-这两台打印机都在我的WiFi网络上找到并从那里安装,结果是:
1.它们都不是“共享的”——它们总是“本地的”
2.连接加农炮时,它也会出现在“EnableBidi”上
3.EnumeratedPrintQueueTypes=512不存在-但给出结果:) 代码如下:
for (int i = 0; i < 20; i++)
{
EnumeratedPrintQueueTypes[] enumerationFlags = { (EnumeratedPrintQueueTypes)Math.Pow(2,i)};
LocalPrintServer printServer = new LocalPrintServer();
//Use the enumerationFlags to filter out unwanted print queues
PrintQueueCollection printQueuesOnLocalServer = printServer.GetPrintQueues(enumerationFlags);
Console.WriteLine("These are your shared, local print queues:\t {0}\n---------------------------\n", enumerationFlags[0]);
foreach (PrintQueue printer in printQueuesOnLocalServer)
{
Console.WriteLine("\t" + printer.Name );
}
Console.WriteLine();
}
为什么不要求安装打印机呢?我不知道你为什么要接管这个功能…@RonBeyer-因为我提供的每个系统都在谁知道哪个国家/地区/办公室等。我不知道那里有哪些打印机和哪些网络。。。这是一个很好的开始。使用EnumeratedPrintQueueTypes获取所需打印机的类型。谢谢@sajithSager。。。但由于某种原因,在我的计算机上运行此代码时(安装了2台打印机&onNote等)-没有结果(
printQueuesOnLocalServer.Count
=0)EnumeratedPrintQueueTypes提供了一个交集而不是并集。因此,如果同时添加EnumeratedPrintQueueTypes.Local和EnumeratedPrintQueueTypes.Shared,它将查找本地和共享的打印机
for (int i = 0; i < 20; i++)
{
EnumeratedPrintQueueTypes[] enumerationFlags = { (EnumeratedPrintQueueTypes)Math.Pow(2,i)};
LocalPrintServer printServer = new LocalPrintServer();
//Use the enumerationFlags to filter out unwanted print queues
PrintQueueCollection printQueuesOnLocalServer = printServer.GetPrintQueues(enumerationFlags);
Console.WriteLine("These are your shared, local print queues:\t {0}\n---------------------------\n", enumerationFlags[0]);
foreach (PrintQueue printer in printQueuesOnLocalServer)
{
Console.WriteLine("\t" + printer.Name );
}
Console.WriteLine();
}
These are your shared, local print queues: Queued
---------------------------
These are your shared, local print queues: DirectPrinting
---------------------------
These are your shared, local print queues: 4
---------------------------
These are your shared, local print queues: Shared
---------------------------
These are your shared, local print queues: Connections
---------------------------
These are your shared, local print queues: 32
---------------------------
These are your shared, local print queues: Local
---------------------------
OneNote
Send To OneNote 2016
Microsoft XPS Document Writer
Microsoft Print to PDF
HP872916 (HP OfficeJet Pro 7740 series)
Fax - HP OfficeJet Pro 7740 series
Fax
These are your shared, local print queues: EnableDevQuery
---------------------------
These are your shared, local print queues: KeepPrintedJobs
---------------------------
These are your shared, local print queues: 512
---------------------------
Send To OneNote 2016
Microsoft XPS Document Writer
Microsoft Print to PDF
HP872916 (HP OfficeJet Pro 7740 series)
These are your shared, local print queues: WorkOffline
---------------------------
These are your shared, local print queues: EnableBidi
---------------------------
These are your shared, local print queues: RawOnly
---------------------------
These are your shared, local print queues: PublishedInDirectoryServices
---------------------------
These are your shared, local print queues: Fax
---------------------------
Fax
These are your shared, local print queues: TerminalServer
---------------------------
These are your shared, local print queues: 65536
---------------------------
These are your shared, local print queues: PushedUserConnection
---------------------------
These are your shared, local print queues: PushedMachineConnection
---------------------------
These are your shared, local print queues: 524288
---------------------------
Press enter to continue.