Reputation: 85
I need to be able to print out a number of documents aytomatically with no user input.
The application is going to sit on a server and print at a certain point each day. I have the requirement to print some documents to 1 printer and some to another printer.
I cannot get my code to print to the non default printer, unless that non-default printer is the Microsoft XPS Document writer, which leads me to suspect permissions.
I am setting the printer with PrinterSettings.PrinterName = "*printername*"
but get a printerexception error saying the settings are incorrect.
I did find an article that metioned a similer error but this was with ASP.Net and not C# - it was to do with Windows Impersonation but I really dont understand what to do to get it working in c#.
I should add that I know the network printer name is correct as I got a little bit of MS Code to show all installed printers and copied the name from the list it provided.
Upvotes: 0
Views: 5489
Reputation: 85
I have just found out that if I print to \servername\printername it works, as opposed to printing to the name of the printer as it shows in control panel. I now have a different issue - if I print to the default printer i.e. dont specift a printer, then it spits them out quickly. If I specify a printer it waits for about a minute then prints, then waits a minute then prints the next one etc - why should it take longer this way?
Upvotes: 0
Reputation: 9394
If you want to set the default-printer you can do it by the winapi with:
[DllImport("winspool.drv", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetDefaultPrinter(string Name);
If you want to reset the defaultprinter after your code is finished you probably need another method from the winapi:
[DllImport("winspool.drv", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetDefaultPrinter(StringBuilder pszBuffer, ref int size);
A list of all available printers you can get by:
List<string> printers = PrinterSettings.InstalledPrinters.Cast<string>().ToList();
Upvotes: 2