Hi all,

I am hoping that someone will be able to point me in the correct direction. I am trying to print an error log (simple txt file) from a c# console application. I have created the txt file and can access and display the information caontained within it. However i need to be able to print the file as well. So far all of the help files and text books i have lloked at have been based on printing using print controls. Is it possible to print without these controls and if so can someone pls point me in the direction of a decent help topic.

Thanks

Dani AI

Generated

Short summary and practical options (builds on 's ShellExecute idea): a C# console app can print a text file several ways — (A) ask Windows to print the file with the shell "print" verb (no extra exe), (B) render text directly with PrintDocument for full control, or (C) send raw bytes to the printer (Win32 WritePrinter) when silent/raw output is required. 's compiled helper works because it calls ShellExecute("print"); the same can be done directly from C# without a separate C++ program. PrintDocument is the best choice when layout, fonts, paging, or silent/background printing is needed.

Quick: print via the shell (uses the associated app's print handler)

// requires: using System.Diagnostics;
var psi = new ProcessStartInfo {
    FileName = @"C:\path\to\log.txt",
    Verb = "print",
    UseShellExecute = true,
    WindowStyle = ProcessWindowStyle.Hidden
};
using (var p = Process.Start(psi)) {
    p?.WaitForExit(15000); // optional timeout
}

Quick: PrintDocument for controlled output (works in .NET Framework on Windows)

// requires: using System.IO; using System.Drawing; using System.Drawing.Printing;
string[] lines = File.ReadAllLines("log.txt");
int idx = 0;
var pd = new PrintDocument();
pd.PrintPage += (s,e) => {
    float y = e.MarginBounds.Top;
    using (var f = new Font("Courier New",10)) {
        float lh = f.GetHeight(e.Graphics);
        while (idx < lines.Length) {
            e.Graphics.DrawString(lines[idx++], f, Brushes.Black, e.MarginBounds.Left, y);
            y += lh;
            if (y + lh > e.MarginBounds.Bottom) { e.HasMorePages = true; return; }
        }
        e.HasMorePages = false;
    }
};
pd.Print();

Notes and cautions: the shell method depends on the file association and may pop UI or print asynchronously. PrintDocument gives layout control but requires System.Drawing.Printing and is Windows/desktop oriented. For services or fully silent/raw output, use a RawPrinterHelper (Win32 WritePrinter) and ensure the process identity has printer access.

Recommended Answers

All 4 Replies

If you do not want to use managed print controls, I suggest you the following. You can improve this solution.

The following code prints a file using default printer in a windows system (you can easily compile that by any C++ compiler, or by a C compiler by modifying the first header)

#include <iostream>
#include <windows.h>

int main(char* arg)
{
    ShellExecute(NULL, "print", arg, NULL, NULL, 0);
}

I tried to use that code directly in C# using InteropServices or Process class which I failed eventually. So you can do that later, if you wish.

Instead of embedding the code, I simply execute the compiled c++ executable using Process class in C# while sending the file name as a parameter to it.

//the following namespace to be used
using System.Diagnostics;


// the code to execute toprinter.exe (compiled c++ code)
        static void Main(string[] args)
        {
            try
            { 
                Console.WriteLine("Input file name");
                string filename = Console.ReadLine();

                Process myProcess = new Process();
                ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("toprinter.exe" );
                myProcessStartInfo.UseShellExecute = false;
                myProcessStartInfo.Arguments = filename;
                myProcess.StartInfo = myProcessStartInfo;
                myProcess.Start();
                myProcess.WaitForExit();
                myProcess.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

Thank you for you reply....sorry it has taken me so long. The c++ has to be complied first? as i have never used it and did not realise i could call in somthing like that

Thanks again though.

is the link to the zipped and compiled "toprinter.exe". Unzip it to the bin folder of your project.

The simple program takes a file name like "example.txt" as a parameter as shown in the c# code.

Later, if you wish, you can try to turn the whole stuff into a dll or use interoperability. But, anyhow, this will solve your problem.

Try to use the class ControlPaint, I'm not sure that achive the deal but try may be that help u

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.