如何在 ASP.NET 中获取网络打印机
我需要显示网络中安装的打印机。我使用下面的代码来显示打印机,但在本地它显示网络打印机,但是当托管在 IIS 中时,它不显示网络打印机。(仅显示本地打印机)。
code -1:
ApplicationPrinter printer = new ApplicationPrinter();
ddlPrinters.DataSource = printer.InstalledPrinters();
ddlPrinters.DataBind();
ddlPrinters.SelectedItem.Text = printer.DefaultZebraPrinter();
code -2:
ddlPrinters.Items.Clear();
try
{
// Use the ObjectQuery to get the list of configured printers
ObjectQuery oquery =
new ObjectQuery("SELECT * FROM Win32_Printer");
ManagementObjectSearcher mosearcher =
new ManagementObjectSearcher(oquery);
ManagementObjectCollection moc = mosearcher.Get();
foreach (ManagementObject mo in moc)
{
PropertyDataCollection pdc = mo.Properties;
foreach (PropertyData pd in pdc)
{
if ((bool)mo["Network"])
{
ddlPrinters.Items.Add(mo["Name"].ToString());
break;
}
}
}
}
catch (ManagementException ex)
{
string msgDesc = string.Empty;
string script = string.Empty;
msgDesc = ex.Message;
script = "<script language=\"javascript\">alert('" + msgDesc + "'); </script>";
ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "UserMessage", script, false);
}
这里 if ((bool)mo["Network"]) 用于仅显示网络打印机。
有没有办法在 ASP.NET 中获取网络打印机?
I need to show printers installed in network. I used the below code to show printers but locally it shows network printers, but when hosted in IIS it doesn't show network printer.(shows only local printers).
code -1:
ApplicationPrinter printer = new ApplicationPrinter();
ddlPrinters.DataSource = printer.InstalledPrinters();
ddlPrinters.DataBind();
ddlPrinters.SelectedItem.Text = printer.DefaultZebraPrinter();
code -2:
ddlPrinters.Items.Clear();
try
{
// Use the ObjectQuery to get the list of configured printers
ObjectQuery oquery =
new ObjectQuery("SELECT * FROM Win32_Printer");
ManagementObjectSearcher mosearcher =
new ManagementObjectSearcher(oquery);
ManagementObjectCollection moc = mosearcher.Get();
foreach (ManagementObject mo in moc)
{
PropertyDataCollection pdc = mo.Properties;
foreach (PropertyData pd in pdc)
{
if ((bool)mo["Network"])
{
ddlPrinters.Items.Add(mo["Name"].ToString());
break;
}
}
}
}
catch (ManagementException ex)
{
string msgDesc = string.Empty;
string script = string.Empty;
msgDesc = ex.Message;
script = "<script language=\"javascript\">alert('" + msgDesc + "'); </script>";
ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "UserMessage", script, false);
}
Here if ((bool)mo["Network"]) is used to show only Network printers.
Is there any way to get network printers in ASP.NET?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将此行添加到 web.config 部分即可解决问题。提供的用户 ID 需要映射网络打印机。
Adding this line to the section of the web.config does the trick. The userid provided needs to have the network printers mapped.
您是否尝试过先在工作站上安装网络打印机,然后执行 win32_printer 查询?
PS你可以通过\\printername安装它
Have you tried installing the network printer on the workstation first, and then do a win32_printer query?
P.s. you can install it by \\printername