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

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.

Here 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.