0

I have the following C# code that successfully prints a file that it is provided. This is in Windows 7.

    // Uses the Default settings of the Windows Environment to open the file and send to printer
    // Seen: http://stackoverflow.com/a/6106155
    public void printPdfHiddenProcess(string filename)
    {
        ProcessStartInfo info = new ProcessStartInfo();
        Process p;

        // Set process setting to be hidden
        info.Verb = "print";
        info.FileName = filename;
        info.CreateNoWindow = true;
        info.WindowStyle = ProcessWindowStyle.Hidden;

        // Start hidden process
        p = new Process();
        p.StartInfo = info;
        p.Start();

        // Give the process some time
        p.WaitForInputIdle();
        Thread.Sleep(1000);

        // Close it
        if (p.CloseMainWindow() == false)
        {
            p.Close();
        }
    }

However, this causes it to print to the default printer. ProcessStartInfo doesn't appear to provide specific methods I can use to pass the printer name, but I might be missing something.

How can I print to a specific printer using a hidden process?

Goose
  • 4,764
  • 5
  • 45
  • 84

1 Answers1

1

Printgoes to the default, to use another you would use PrintTo and name it. Similar to Save vs Save As

info.Verb = "PrintTo";               // was "Print"
string PrinterName = "Some Printer"; // add printer specific name here...
info.Arguments = PrinterName;

More info: Print documents... using the printto verb

Mad Myche
  • 1,075
  • 1
  • 7
  • 15
  • Looks promising but it isn't working for me yet. Will read that link and see if I can figure it out. – Goose May 01 '17 at 20:28
  • I got it working! I had to change info.Arguments, I passed the printer name directly as in `info.Arguments = printerName;` and it worked. If you can update your answer to at least note this I'll gladly accept. – Goose May 01 '17 at 20:36
  • Updated answer with your comments – Mad Myche May 01 '17 at 21:22