在 C# 中确定打印机的 IP 地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/676863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determine the IP Address of a Printer in C#
提问by MagicAndi
I would like to determine the IP address of a printer, using C# (.NET 2.0). I have only the printer share name as set up on the Windows OS, in the format \\PC Name\Printer Name
. The printer is a network printer, and has a different IP address to the PC. Does anyone have any pointers?
我想使用 C# (.NET 2.0) 确定打印机的 IP 地址。我只有在 Windows 操作系统上设置的打印机共享名称,格式为\\PC Name\Printer Name
. 打印机是网络打印机,与 PC 有不同的 IP 地址。有没有人有任何指示?
Thanks in advance for your help.
在此先感谢您的帮助。
Regards, Andy.
问候,安迪。
采纳答案by Panos
Check this question: How to get Printer Info in C#.NET?. I think that you have to get the property PortName
from the WMI properties.
检查这个问题:如何在 C#.NET 中获取打印机信息?. 我认为您必须PortName
从 WMI 属性中获取该属性。
回答by Irfy
Is this printer set up in a network which has Active Directory? Or is this on your own local network with just a switch and your printer plugged into it?
这台打印机是否设置在具有 Active Directory 的网络中?或者这是在您自己的本地网络上,只有一个交换机和您的打印机插入其中?
If it is the former, then you should be able to query for it based on the "printer name". This article show how to get c# .net to connect to the AD. But this does require some knowledge of AD servers in your network.
如果是前者,那么您应该可以根据“打印机名称”进行查询。本文展示了如何让 c# .net 连接到 AD。但这确实需要您了解网络中的 AD 服务器。
This solution seems a bit long to me, but may be a good starting point?
这个解决方案对我来说似乎有点长,但可能是一个很好的起点?
回答by MagicAndi
Based on the link How to get Printer Info in .NET?(Thanks, Panos, I was already looking at the link!), I have the following solution from Panos'sanswer:
基于链接如何在 .NET 中获取打印机信息?(谢谢,帕诺斯,我已经在看链接了!),我从帕诺斯的回答中得到了以下解决方案:
using System.Management;
...
string printerName = "YourPrinterName";
string query = string.Format("SELECT * from Win32_Printer WHERE Name LIKE '%{0}'", printerName);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection coll = searcher.Get();
foreach (ManagementObject printer in coll)
{
string portName = printer["PortName"].ToString();
if(portName.StartsWith("IP_"))
{
Console.WriteLine(string.Format("Printer IP Address: {0}", portName.Substring(3)));
}
}
Obviously, this only works if the port name for the printer is given in the format "IP_IPAddress", which is I believe is the default.
显然,这仅在打印机的端口名称以“IP_IPAddress”格式给出时才有效,我认为这是默认设置。
回答by Michael Ball
I know this is an old post, but I had the same issue where I was able to get the Printer Port name, but not the IP. In my case I couldn't rely on the Port Name being IP_[IP Address] but found how to get hold of the actual IP from the port name.
我知道这是一篇旧帖子,但我遇到了同样的问题,我能够获得打印机端口名称,但无法获得 IP。在我的情况下,我不能依赖端口名称是 IP_[IP 地址],但找到了如何从端口名称获取实际 IP。
Windows stores the information about ports in the registry under
Windows 将有关端口的信息存储在注册表中
HKLM\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\[port name]
HKLM\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\[端口名称]
This key contains the values set up in the port configuration page, including IP address and port number.
此键包含在端口配置页面中设置的值,包括 IP 地址和端口号。
A quick C# example to get the IP address
获取 IP 地址的快速 C# 示例
using Microsoft.Win32;
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\" + printerPortName, RegistryKeyPermissionCheck.Default, System.Security.AccessControl.RegistryRights.QueryValues);
if (key != null)
{
String IP = (String)key.GetValue("IPAddress", String.Empty, RegistryValueOptions.DoNotExpandEnvironmentNames);
}
回答by Jay
Just adding an another solution here using .Net Framework 4.0 or higher
只需在此处使用 .Net Framework 4.0 或更高版本添加另一个解决方案
Using System.Printing
var server = new PrintServer();
var queues = server.GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Connections });
foreach (var queue in queues)
{
string printerName = queue.Name;
string printerPort = queue.QueuePort.Name;
}
回答by Alexander A. Sharygin
using of class WIN32_Printer is not enough here. It should be combined with Win32_TCPIPPrinterPort.
在这里使用 WIN32_Printer 类是不够的。它应该与 Win32_TCPIPPrinterPort 结合使用。
Below is the code which should help:
下面是应该有帮助的代码:
static void Main(string[] args)
{
var scope = new ManagementScope(@"\root\cimv2");
scope.Connect();
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer");
var results = searcher.Get();
Console.WriteLine("Network printers list:");
foreach (var printer in results)
{
var portName = printer.Properties["PortName"].Value;
var searcher2 = new ManagementObjectSearcher("SELECT * FROM Win32_TCPIPPrinterPort where Name LIKE '" + portName + "'");
var results2 = searcher2.Get();
foreach (var printer2 in results2)
{
Console.WriteLine("Name:" + printer.Properties["Name"].Value);
//Console.WriteLine("PortName:" + portName);
Console.WriteLine("PortNumber:" + printer2.Properties["PortNumber"].Value);
Console.WriteLine("HostAddress:" + printer2.Properties["HostAddress"].Value);
}
Console.WriteLine();
}
Console.ReadLine();
}