none
Configuring printer port from c# code

    Question

  • I need to configure a virtual printer port to redirect it to a external program(.exe file). The attached picture illustrates the requirement. Any help will be highly appreciated.

    Configuring printer port to redirect to external program

    Tuesday, May 01, 2012 7:17 PM

Answers

  • I found out solution to the above problem.
    All the printer ports registered on the system are listed in registry under the key "SYSTEM\\ControlSet001\\Control\\Print\\Monitors\\Redirected Port\\Ports"

    Values under these keys can be edited to get the desired result. Below is the code to edit it using 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();
    Tuesday, May 01, 2012 10:49 PM

All replies

  • I found out solution to the above problem.
    All the printer ports registered on the system are listed in registry under the key "SYSTEM\\ControlSet001\\Control\\Print\\Monitors\\Redirected Port\\Ports"

    Values under these keys can be edited to get the desired result. Below is the code to edit it using 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();
    Tuesday, May 01, 2012 10:49 PM
  • Congratulations. Thank you for sharing this solution here, it will be very benefit to the other community members.

    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Wednesday, May 02, 2012 10:30 AM