tags:

views:

501

answers:

3

Hi,

Is there a way to send a .txt to a printer using c#?

something like

string doc = "c:\temp.txt";
sendToPrinter(doc);
A: 

This is what I've used recently.

public void Print(FileInfo file)
{
    ProcessStartInfo psi = new ProcessStartInfo();
    psi.FileName = "print";
    psi.Arguments = string.Format("/D:\"{0}\" \"{1}\"", Printer, file.FullName);
    psi.CreateNoWindow = true;
    psi.UseShellExecute = false;

    Process p = new Process();
    p.StartInfo = psi;
    p.Start();

    while (!p.HasExited) ;
}
Austin Salonen
does this print to the default printer
*Printer* is a string property of the class. I believe if you remove the "/D:" argument, it will send to the default printer.
Austin Salonen
Maybe because I was printing to a virtual printer. This command tells me unable to initialize device.
+1  A: 

MSDN

luvieere
+1  A: 

MS has a newer document for .NET on How to: Print a Multi-Page Text File in Windows Forms for .NET 2.0, 3.0, 3.5, and 4.0.

It's essentially a newer, more complete version of the document in luvieere's answer, which was written for .NET 1.1

R. Bemrose