You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何查询Net Use映射的LPT1端口所连接的网络打印机?

查询LPT1端口映射的打印机信息

我在程序中映射了一台标签网络打印机,想知道当前LPT1端口连接的具体打印机设备。以下是我的打印机映射函数,请问有什么方法可以查询当前LPT1端口所连接的打印机信息?期望输出示例:您已连接到打印机"DESKTOP01"

public void impressora(string computador)
{
    System.Diagnostics.Process process = new System.Diagnostics.Process();
    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();

    startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    startInfo.FileName = "cmd.exe";
    startInfo.Arguments = $@"/c net use lpt1: /delete & net use lpt1: \\{computador}\zpl";
    process.StartInfo = startInfo;
    process.Start();
}

解决方案

方法1:执行net use命令解析输出

通过执行net use lpt1:命令获取映射信息,再解析输出提取打印机所在的计算机名称:

public string GetLpt1PrinterInfo()
{
    var process = new System.Diagnostics.Process();
    var startInfo = new System.Diagnostics.ProcessStartInfo
    {
        WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
        FileName = "cmd.exe",
        Arguments = "/c net use lpt1:",
        RedirectStandardOutput = true,
        UseShellExecute = false,
        CreateNoWindow = true
    };

    process.StartInfo = startInfo;
    process.Start();
    
    string output = process.StandardOutput.ReadToEnd();
    process.WaitForExit();

    // 匹配\\之后的计算机名称部分
    var match = System.Text.RegularExpressions.Regex.Match(output, @"\\\\([^\\]+)\\");
    if (match.Success)
    {
        string printerHost = match.Groups[1].Value;
        return $"您已连接到打印机\"{printerHost}\"";
    }
    else
    {
        return "LPT1端口未映射任何打印机";
    }
}

方法2:通过WMI查询映射记录

利用WMI的Win32_NetworkConnection类直接查询LPT1的网络连接信息:

using System.Management;

public string GetLpt1PrinterViaWmi()
{
    try
    {
        var searcher = new ManagementObjectSearcher("SELECT RemoteName FROM Win32_NetworkConnection WHERE LocalName = 'LPT1:'");
        foreach (ManagementObject obj in searcher.Get())
        {
            string remoteName = obj["RemoteName"]?.ToString();
            if (!string.IsNullOrEmpty(remoteName))
            {
                var pathParts = remoteName.Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
                if (pathParts.Length >= 1)
                {
                    return $"您已连接到打印机\"{pathParts[0]}\"";
                }
            }
        }
        return "LPT1端口未映射任何打印机";
    }
    catch (ManagementException ex)
    {
        return $"查询失败:{ex.Message}";
    }
}

方法3:读取注册表信息

网络端口的映射信息存储在注册表HKEY_CURRENT_USER\Network\LPT1路径下,可读取RemotePath值解析:

using Microsoft.Win32;

public string GetLpt1PrinterFromRegistry()
{
    using (RegistryKey lptKey = Registry.CurrentUser.OpenSubKey("Network\\LPT1"))
    {
        if (lptKey != null)
        {
            string remotePath = lptKey.GetValue("RemotePath")?.ToString();
            if (!string.IsNullOrEmpty(remotePath))
            {
                var pathParts = remotePath.Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
                if (pathParts.Length >= 1)
                {
                    return $"您已连接到打印机\"{pathParts[0]}\"";
                }
            }
        }
        return "LPT1端口未映射任何打印机";
    }
}

内容的提问来源于stack exchange,提问作者Mateus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 04:05:25