1

I have been tasked with creating a WCF service that will print a specified file to a network printer. I am using the System.Drawing method in C# to create the print method however when I run through it I keep getting the following error message:

{"Settings to access printer '\\\\UKPRINT01\\UKPRINT024' are not valid"}

I have looked online and the only advice I can find is having the printer installed locally however this isn't really a viable option for this particular WCF service. The WCF will be hosted via Windows Service, and it is being created using .net 4.0 and in Visual Studio 2012.

The code for printing method is as follows:

streamToPrint = new StreamReader(filePath);
try
{  
        printFont = new Font("Arial", 10);
        PrintDocument pd = new PrintDocument();
        pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
        pd.PrinterSettings.PrinterName = @"\\UKSPPS01\UKSPPRN024";
        // Print the document.
        pd.Print();
}
finally
{
    streamToPrint.Close();
}
Kapil Khandelwal
  • 15,958
  • 2
  • 45
  • 52
  • 1
    why don't you just add the printer to your windows network printers and than print to it? – Nahum Apr 09 '13 at 10:15
  • I'm not sure you can use the `PrintDocument` API from a Windows Service. Maybe you should try hosting the WCF service in Winforms application. – John Saunders Apr 09 '13 at 10:16
  • 1
    FYI, you should probably put the `StreamReader` in a `using` block instead of the try/finally. – John Saunders Apr 09 '13 at 10:18
  • do you have access to the printer – user1659922 Apr 09 '13 at 10:29
  • @JohnSaunders thanks for the tip on the using statement. With this particular option I am actually creating the print method in a console application (as I thought it might have been the WCF that was the problem) for testing, unfortunately this still isn't working correctly so I'm thinking it must be something to with the settings, but I can't really pin-point where the problem might be. – SymbioticKaos Apr 09 '13 at 10:30
  • @user1659922 yes I can print to it no problem – SymbioticKaos Apr 09 '13 at 10:32
  • Just a permissions issue, see for example [Error "Settings to access printer 'printername' are not valid" when printing from Windows service](http://stackoverflow.com/questions/2722327/error-settings-to-access-printer-printername-are-not-valid-when-printing-fro). – CodeCaster Apr 09 '13 at 10:33
  • @CodeCaster This solution worked perfectly, could you post it as an answer so that I can mark it. Thanks. – SymbioticKaos Apr 10 '13 at 07:47

0 Answers0