|
using System;
using System.IO;
using System.Collections;
using PRNADMINLib;
namespace PrinterExample
{
/// <summary>
/// Example program will set the system default to print to a file.
/// The command line will take a printer name, a file name, and a driver name
/// to be used when a new printer is created. If the specified printer or
/// port do not exist, then they will be created.
/// </summary>
public class PrinterExample
{
// Default Configuration
private static string portfileName = "C:\\postscript.ps";
private static string printerName = "TESTPRINTER";
private static string newPrinterDriver = "HP DeskJet 1200C/PS";
// Empty string object to specify local system in COM calls
private static object oServerName = ""; // local system
// Main program
public static void Main(string[] args)
{
// Check arguments and set portfile name
if (args.Length > 3)
{
Console.WriteLine("Usage: PrinterExample.exe [printer [filename [driver]]]");
return;
}
if (args.Length > 0) printerName = args[0];
if (args.Length > 1) portfileName = args[1];
if (args.Length > 2) newPrinterDriver = args[2];
// Check existence of portfile folder
FileInfo portfileinfo = new FileInfo(portfileName);
DirectoryInfo portFolder = portfileinfo.Directory;
if (!portFolder.Exists)
{
Console.WriteLine("Error: Portfile folder " +
portFolder + " does not exist.");
return;
}
// Get the normalized pathname
portfileName = portfileinfo.FullName;
// Get a reference to the printer subsystem
PRNADMINLib.PrintMaster pMaster = new PRNADMINLib.PrintMasterClass();
// Check existence of port
PRNADMINLib.Port pPort = null;
PRNADMINLib.PortCollection portColl =
(PRNADMINLib.PortCollection)pMaster.get_Ports(ref oServerName);
IEnumerator portEnumerator = portColl.GetEnumerator();
portEnumerator.Reset();
while (portEnumerator.MoveNext())
{
PRNADMINLib.Port portCurrent = portEnumerator.Current as PRNADMINLib.Port;
if (portCurrent.PortName == portfileName)
{
pPort = portCurrent;
break;
}
}
// Get reference to port or create it if it does not exist
if (pPort == null)
{
pPort = new PRNADMINLib.PortClass();
pPort.PortName = portfileName;
pPort.PortType = 3; // Simple local port
pMaster.PortAdd(pPort);
}
// Check existence of printer
PRNADMINLib.Printer pPrinter = null;
PRNADMINLib.PrinterCollection prnColl =
(PRNADMINLib.PrinterCollection)pMaster.get_Printers(ref oServerName);
IEnumerator prnEnumerator = prnColl.GetEnumerator();
prnEnumerator.Reset();
while (prnEnumerator.MoveNext())
{
PRNADMINLib.Printer prnCurrent =
prnEnumerator.Current as PRNADMINLib.Printer;
if (prnCurrent.PrinterName == printerName)
{
pPrinter = prnCurrent;
break;
}
}
// Get reference to printer and create it if it does not exist
if (pPrinter == null)
{
// To add printer, name, driver, and port are required
pPrinter = new PRNADMINLib.PrinterClass();
pPrinter.PrinterName = printerName;
pPrinter.DriverName = newPrinterDriver;
pPrinter.PortName = pPort.PortName;
pMaster.PrinterAdd(pPrinter);
}
else
{
// Make sure port is set on existing printer
if (pPrinter.PortName != pPort.PortName)
{
pPrinter.PortName = pPort.PortName;
}
// Driver cannot be re-set on existing printer
if (args.Length == 3 && newPrinterDriver != pPrinter.DriverName)
{
Console.WriteLine("Warning: Driver remains set to " + pPrinter.DriverName);
}
pMaster.PrinterSet(pPrinter);
}
// Set printer as system default
pMaster.DefaultPrinter = pPrinter.PrinterName;
// Print test page
pMaster.PrintTestPage("", pPrinter.PrinterName);
// Let us know what happened...
Console.WriteLine("Results for " + printerName + ":");
Console.WriteLine(" Port set to " + portfileName);
Console.WriteLine(" Using driver \"" + pPrinter.DriverName + "\"");
Console.WriteLine(" " + printerName + " set to default printer.");
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.