【发布时间】:2019-05-13 16:53:34
【问题描述】:
在我的应用程序(win10、WPF)上,我让用户可以打印报告。
但是
当本地机器上有尚未配置的打印机时 - 这是不可能的。
(当然打印机是在网络上激活的,通过settings -> add printer & sccaners可以找到并安装)
我已经在网上搜索了几天,正在寻找一种方法:
1.
使用ManagementObjectSearcher("SELECT * from Win32_Printer")
但只有 localy 安装了打印选项
2.
尝试了以下代码 - 但我不知道服务器或打印机名称
(我可能知道用户运行我的应用程序的网络名称 - 但它可以是任何类型的打印机......)
using (ManagementClass win32Printer = new ManagementClass("Win32_Printer"))
{
using (ManagementBaseObject inputParam = win32Printer.GetMethodParameters("AddPrinterConnection"))
{
// Replace <server_name> and <printer_name> with the actual server and
// printer names.
inputParam.SetPropertyValue("Name", "\\\\<server_name>\\<printer_name>");
using (ManagementBaseObject result =
(ManagementBaseObject)win32Printer.InvokeMethod("AddPrinterConnection", inputParam, null))
{
uint errorCode = (uint)result.Properties["returnValue"].Value;
switch (errorCode)
{
case 0:
Console.Out.WriteLine("Successfully connected printer.");
break;
case 5:
Console.Out.WriteLine("Access Denied.");
break;
case 123:
Console.Out.WriteLine("The filename, directory name, or volume label syntax is incorrect.");
break;
case 1801:
Console.Out.WriteLine("Invalid Printer Name.");
break;
case 1930:
Console.Out.WriteLine("Incompatible Printer Driver.");
break;
case 3019:
Console.Out.WriteLine("The specified printer driver was not found on the system and needs to be downloaded.");
break;
}
}
}
}
在安装打印机驱动器(从 Windows
add printers & scanners)后,我得到了打印机的 mac 地址,然后即使在卸载后我也可以获得它的 IP 地址(建议here)
以上都没有帮到我...
因此我正在寻找以编程方式安装新打印机的任何可能方式 - 例如:
- 使用“添加新打印机”选项启动
PrintDialog - 使用
printers & scanners屏幕打开windows settings窗口
发现:Process.Start("ms-settings:printers"); - 使用上述任何数据安装新打印机
- 以任何其他方式进行....... :)
【问题讨论】:
-
为什么不要求安装打印机?我不知道你为什么要接管这个功能......
-
@RonBeyer - 因为我交付的每个系统都将在谁知道的国家/办事处等。我不知道那里有哪些打印机以及哪些网络......
-
This 将是一个很好的起点。使用 EnumeratedPrintQueueTypes 获取您想要的打印机类型。
-
感谢@SajithSageer ...但是由于某种原因在我的计算机上运行此代码时(安装了2台打印机和onNote等)-没有结果(
printQueuesOnLocalServer.Count= 0)跨度> -
EnumeratedPrintQueueTypes 给出交集而不是联合。因此,如果您同时添加 EnumeratedPrintQueueTypes.Local、EnumeratedPrintQueueTypes.Shared,它将查找本地和共享的打印机
标签: c# wpf printers network-printers printdialog