Skip to main content
 首页 » 编程设计

c#之配置虚拟打印机端口重定向

2024年02月23日15www_RR

我需要配置一个虚拟打印机端口,以通过 C# 代码将其重定向到外部程序(.exe 文件)。 现在我可以安装一个带有一些自定义的虚拟端口(感谢 bghh 代码)。 所附图片说明了要求。任何帮助将不胜感激。

请您参考如下方法:

我找到了上述问题的解决方案。 在系统上注册的所有打印机端口都列在注册表项“SYSTEM\ControlSet001\Control\Print\Monitors\Redirected Port\Ports”

下

可以编辑这些键下的值以获得所需的结果。下面是使用 C# 编辑它的代码。

bool found = false; 
string portName = "testing:"; 
RegistryKey PrinterPort = Registry.LocalMachine.OpenSubKey("SYSTEM\\ControlSet001\\Control\\Print\\Monitors\\Redirected Port\\Ports", true); 
foreach (string pp in PrinterPort.GetSubKeyNames()) 
{ 
    if (pp == portName) 
    { 
        PrinterPort = Registry.LocalMachine.OpenSubKey("SYSTEM\\ControlSet001\\Control\\Print\\Monitors\\Redirected Port\\Ports"+"\\"+portName, true); 
        found = true; break; 
    } 
} 
if (found) 
{ 
    PrinterPort.SetValue(@"Arguments", "@C:\\gs\\pdfwrite.txt -sOutputFile=\"d:\\hello.pdf\" -c .setpdfwrite -f -"); 
    PrinterPort.SetValue(@"Command", "c:\\gs\\bin\\gswin32c.exe"); 
    PrinterPort.SetValue(@"Delay", 0x12c); 
    PrinterPort.SetValue(@"LogFileDebug", 0x0); 
    PrinterPort.SetValue(@"LogFileName", ""); 
    PrinterPort.SetValue(@"LogFileUse", 0x0); 
    PrinterPort.SetValue(@"Output", 0x0); 
    PrinterPort.SetValue(@"Printer", "Send To Cool Printer"); 
    PrinterPort.SetValue(@"PrintError", 0x0); 
    PrinterPort.SetValue(@"RunUser", 0x0); 
    PrinterPort.SetValue(@"ShowWindow", 0x0); 
} 
PrinterPort.Close();