1.11M Members

Printing from a console application

 
0
 

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

 
0
 

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);
            }
        }
 
0
 

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.

 
0
 

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.

 
0
 

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

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article