从 Win32_TCPIPPrinterPort 检索主机地址时出现问题

发布于 2024-07-16 18:33:44 字数 654 浏览 3 评论 0原文

我在检索打印机端口地址时遇到了一个奇怪的问题。 当我获取 Win32_TCPIPPrinterPort 中的所有条目时,HostAddress 字段(应具有 IP 地址)通常为空/空,只有端口名称有值。 有点奇怪的是,如果任何打印机都没有使用某个特定端口,则 HostAddress 将具有正确的值。

C# 代码很简单,结果如下; IP_192.168.1.100, Printerportxyz,

richTextBox1.Clear();
ManagementObjectSearcher portSearcher = new ManagementObjectSearcher("root\\CIMV2",
    "SELECT * FROM Win32_TCPIPPrinterPort");
foreach (ManagementObject port in portSearcher.Get())
{
    richTextBox1.AppendText(
        String.Format("Name: {0} HostAddress: {1}",
            port["Name"],
            port["HostAddress"])
        );
}

我也在 WSH/VBS 中尝试了同样的操作,并看到了相同的行为。

I'm running into an odd issue retrieving printer port addresses.
When I get all the entries in Win32_TCPIPPrinterPort, the HostAddress field (which should have the IP address) is usually blank/null, only the port name has a value. To make it a bit stranger, if a particular port is not in use by any printer, THEN the HostAddress will have the the proper value.

The C# code is simple, and results in something like this;
IP_192.168.1.100,
printerportxyz,

richTextBox1.Clear();
ManagementObjectSearcher portSearcher = new ManagementObjectSearcher("root\\CIMV2",
    "SELECT * FROM Win32_TCPIPPrinterPort");
foreach (ManagementObject port in portSearcher.Get())
{
    richTextBox1.AppendText(
        String.Format("Name: {0} HostAddress: {1}",
            port["Name"],
            port["HostAddress"])
        );
}

I also tried the same thing in WSH/VBS, and saw the same behavior.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苦妄 2024-07-23 18:33:44

我最终不得不重新审视这一点,并进行另一次尝试。 我发现内置的 prnport.vbs 管理脚本没有问题 - 调查它我发现在建立其 WMI 连接时它有 oService.Security_.Priveleges.AddAsString "SeLoadDriverPrivilege"

C# 中的解决方案最终指定了 WMI ConnectionOptions 和将 EnablePrivileges 设置为 true。 然后,对于未使用或正在使用的端口,HostAdress 不再为空。

ConnectionOptions connOptions = new ConnectionOptions();
connOptions.EnablePrivileges = true;

ManagementScope mgmtScope = new ManagementScope("root\\CIMV2", connOptions);
mgmtScope.Connect();

ObjectQuery objQuery = new ObjectQuery("SELECT * FROM Win32_TCPIPPrinterPort");
ManagementObjectSearcher moSearcher = new ManagementObjectSearcher(mgmtScope, objQuery);

foreach (ManagementObject mo in moSearcher.Get())
{
    Console.WriteLine(String.Format("PortName={0} HostAddress={1}", mo["Name"], mo["HostAddress"]));
}

I ended up having to re-visit this, and making another attempt. I found that the built-in prnport.vbs managment script had no issues - looking into it I saw that while establishing its WMI connection it had oService.Security_.Priveleges.AddAsString "SeLoadDriverPrivilege"

the solution in C# ended up specifying the WMI ConnectionOptions and setting EnablePrivileges to true. Then the HostAdress was no longer null for unused or in-use ports.

ConnectionOptions connOptions = new ConnectionOptions();
connOptions.EnablePrivileges = true;

ManagementScope mgmtScope = new ManagementScope("root\\CIMV2", connOptions);
mgmtScope.Connect();

ObjectQuery objQuery = new ObjectQuery("SELECT * FROM Win32_TCPIPPrinterPort");
ManagementObjectSearcher moSearcher = new ManagementObjectSearcher(mgmtScope, objQuery);

foreach (ManagementObject mo in moSearcher.Get())
{
    Console.WriteLine(String.Format("PortName={0} HostAddress={1}", mo["Name"], mo["HostAddress"]));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文